-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserver.go
66 lines (54 loc) · 1.39 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"context"
"net"
"net/http"
"strings"
)
var Server_Status bool = false
var Server_httpListen net.Listener
type httpServerHandler struct {
}
func (h *httpServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(405)
w.Write([]byte("405: Method Not Allowed."))
return
}
if currentClientType == "Transmission" && Tr_ProcessHTTP(w, r) {
return
}
w.WriteHeader(404)
w.Write([]byte("404: Not Found."))
}
func StartServer() {
if Server_Status {
return
}
listenType := "tcp4"
if IsIPv6(config.Listen) {
listenType = "tcp6"
}
httpListen, err := net.Listen(listenType, strings.SplitN(config.Listen, "/", 2)[0])
if err != nil {
Log("StartServer", GetLangText("Error-StartServer_Listen"), true, err.Error())
return
}
Server_Status = true
Server_httpListen = httpListen
httpServer.SetKeepAlivesEnabled(false)
if err := httpServer.Serve(Server_httpListen); err != http.ErrServerClosed {
Log("StartServer", GetLangText("Error-StartServer_Serve"), true, err.Error())
Server_Status = false
}
}
func StopServer() {
if !Server_Status {
return
}
if err := httpServer.Shutdown(context.Background()); err != nil {
Log("StopServer", GetLangText("Error-StopServer"), true, err.Error())
}
// 出于保险起见, 再次关闭似乎已被 httpServer 同时关闭的 Listener, 但无视错误.
Server_httpListen.Close()
}