diff --git a/common/netutil/netutil.go b/common/netutil/netutil.go index 82b781d..5cac7d9 100644 --- a/common/netutil/netutil.go +++ b/common/netutil/netutil.go @@ -156,7 +156,7 @@ func GetSrcKey(packet []byte) string { return key } -// GetdstKey returns the destination key of the packets +// GetDstKey returns the destination key of the packets func GetDstKey(packet []byte) string { key := "" if IsIPv4(packet) && len(packet) >= 20 { @@ -167,7 +167,7 @@ func GetDstKey(packet []byte) string { return key } -// ExecuteCommand executes the given command +// ExecCmd executes the given command func ExecCmd(c string, args ...string) string { //log.Printf("exec %v %v", c, args) cmd := exec.Command(c, args...) @@ -214,6 +214,19 @@ func GetDefaultHttpResponse() []byte { return []byte("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 6\r\nConnection: keep-alive\r\nCache-Control: no-cache\r\nCF-Cache-Status: DYNAMIC\r\nServer: cloudflare\r\n\r\nfollow") } +func GetDefaultHttpHandleFunc() http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Length", "6") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("CF-Cache-Status", "DYNAMIC") + w.Header().Set("Server", "cloudflare") + w.Write([]byte("follow")) + }) +} + // PrintErr returns the error log func PrintErr(err error, enableVerbose bool) { if !enableVerbose { diff --git a/h1/tools.go b/h1/conn.go similarity index 100% rename from h1/tools.go rename to h1/conn.go diff --git a/h1/h1server.go b/h1/h1server.go index c325980..dc11e41 100644 --- a/h1/h1server.go +++ b/h1/h1server.go @@ -22,8 +22,8 @@ import ( // StartServer starts the h1 server func StartServer(iFace *water.Interface, config config.Config) { log.Printf("vtun h1 server started on %v", config.LocalAddr) - websrv := NewHandle(http.NotFoundHandler()) - http.Handle("/", websrv) + webSrv := NewHandle(netutil.GetDefaultHttpHandleFunc()) + http.Handle("/", webSrv) srv := &http.Server{Addr: config.LocalAddr, Handler: nil} go func(srv *http.Server) { var err error @@ -53,7 +53,7 @@ func StartServer(iFace *water.Interface, config config.Config) { go toClient(config, iFace) // client -> server for { - conn, err := websrv.Accept() + conn, err := webSrv.Accept() if err != nil { continue } diff --git a/register/register.go b/register/register.go index 76fd6f6..c5693b5 100644 --- a/register/register.go +++ b/register/register.go @@ -32,7 +32,7 @@ func ExistClientIP(ip string) bool { return ok } -// keepAlive keeps the client ip alive +// KeepAliveClientIP keeps the client ip alive func KeepAliveClientIP(ip string) { if ExistClientIP(ip) { _register.Increment(ip, 1) @@ -67,7 +67,7 @@ func PickClientIP(cidr string) (clientIP string, prefixLength string) { // ListClientIPs returns the client ips in the register func ListClientIPs() []string { - result := []string{} + var result []string for k := range _register.Items() { result = append(result, k) } @@ -82,7 +82,7 @@ func addressCount(network *net.IPNet) uint64 { // incr increments the ip by 1 func incr(IP net.IP) net.IP { - IP = checkIPv4(IP) + //IP = checkIPv4(IP) incIP := make([]byte, len(IP)) copy(incIP, IP) for j := len(incIP) - 1; j >= 0; j-- { @@ -101,3 +101,11 @@ func checkIPv4(ip net.IP) net.IP { } return ip } + +// checkIPv6 checks if the ip is IPv6 +func checkIPv6(ip net.IP) net.IP { + if v6 := ip.To16(); v6 != nil { + return v6 + } + return ip +} diff --git a/ws/wsserver.go b/ws/wsserver.go index 5ddcfea..3498a0d 100644 --- a/ws/wsserver.go +++ b/ws/wsserver.go @@ -39,7 +39,13 @@ func StartServer(iface *water.Interface, config config.Config) { }) http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - w.WriteHeader(200) + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Length", "6") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("CF-Cache-Status", "DYNAMIC") + w.Header().Set("Server", "cloudflare") w.Write([]byte(`follow`)) })