mirror of
https://github.com/net-byte/vtun
synced 2024-03-14 10:50:03 +08:00
optimise code.
This commit is contained in:
parent
0e83cdbc27
commit
af319b013d
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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`))
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user