vtun/ws/wsserver.go
2021-09-13 13:32:26 +08:00

161 lines
3.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ws
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/net-byte/vtun/common/cipher"
"github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/register"
"github.com/net-byte/vtun/tun"
"github.com/patrickmn/go-cache"
"github.com/songgao/water"
"github.com/songgao/water/waterutil"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1500,
WriteBufferSize: 1500,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// StartServer starts ws server
func StartServer(config config.Config) {
iface := tun.CreateTun(config)
c := cache.New(30*time.Minute, 10*time.Minute)
go tunToClientWs(config, iface, c)
log.Printf("vtun ws server started on %v,CIDR is %v", config.LocalAddr, config.CIDR)
http.HandleFunc("/way-to-freedom", func(w http.ResponseWriter, r *http.Request) {
wsConn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
wsToServerTun(config, wsConn, iface, c)
})
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello世界")
})
http.HandleFunc("/ip", func(w http.ResponseWriter, req *http.Request) {
ip := req.Header.Get("X-Forwarded-For")
if ip == "" {
ip = strings.Split(req.RemoteAddr, ":")[0]
}
resp := fmt.Sprintf("%v", ip)
io.WriteString(w, resp)
})
http.HandleFunc("/register/pick/ip", func(w http.ResponseWriter, req *http.Request) {
key := req.Header.Get("key")
if key != config.Key {
error403(w, req)
return
}
ip, pl := register.PickClientIP(config.CIDR)
resp := fmt.Sprintf("%v/%v", ip, pl)
io.WriteString(w, resp)
})
http.HandleFunc("/register/delete/ip", func(w http.ResponseWriter, req *http.Request) {
key := req.Header.Get("key")
if key != config.Key {
error403(w, req)
return
}
ip := req.URL.Query().Get("ip")
if ip != "" {
register.DeleteClientIP(ip)
}
io.WriteString(w, "OK")
})
http.HandleFunc("/register/keepalive/ip", func(w http.ResponseWriter, req *http.Request) {
key := req.Header.Get("key")
if key != config.Key {
error403(w, req)
return
}
ip := req.URL.Query().Get("ip")
if ip != "" {
register.KeepAliveClientIP(ip)
}
io.WriteString(w, "OK")
})
http.HandleFunc("/register/list/ip", func(w http.ResponseWriter, req *http.Request) {
key := req.Header.Get("key")
if key != config.Key {
error403(w, req)
return
}
io.WriteString(w, strings.Join(register.ListClientIP(), "\r\n"))
})
http.ListenAndServe(config.LocalAddr, nil)
}
func error403(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("403 No Permission"))
}
func tunToClientWs(config config.Config, iface *water.Interface, c *cache.Cache) {
buffer := make([]byte, 1500)
for {
n, err := iface.Read(buffer)
if err != nil || err == io.EOF || n == 0 {
continue
}
b := buffer[:n]
if !waterutil.IsIPv4(b) {
continue
}
srcAddr, dstAddr := netutil.GetAddr(b)
if srcAddr == "" || dstAddr == "" {
continue
}
key := fmt.Sprintf("%v->%v", dstAddr, srcAddr)
v, ok := c.Get(key)
if ok {
if config.Encrypt {
b = cipher.XOR(b)
}
v.(*websocket.Conn).WriteMessage(websocket.BinaryMessage, b)
}
}
}
func wsToServerTun(config config.Config, wsConn *websocket.Conn, iface *water.Interface, c *cache.Cache) {
defer netutil.CloseWS(wsConn)
for {
wsConn.SetReadDeadline(time.Now().Add(time.Duration(30) * time.Second))
_, b, err := wsConn.ReadMessage()
if err != nil || err == io.EOF {
break
}
if config.Encrypt {
b = cipher.XOR(b)
}
if !waterutil.IsIPv4(b) {
continue
}
srcAddr, dstAddr := netutil.GetAddr(b)
if srcAddr == "" || dstAddr == "" {
continue
}
key := fmt.Sprintf("%v->%v", srcAddr, dstAddr)
c.Set(key, wsConn, cache.DefaultExpiration)
iface.Write(b[:])
}
}