forked from ginuerzh/gost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.go
82 lines (67 loc) · 1.4 KB
/
ws.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"github.com/ginuerzh/gosocks5"
"github.com/gorilla/websocket"
"log"
"net/http"
"time"
)
type WSConn struct {
*websocket.Conn
rb []byte
}
func NewWSConn(conn *websocket.Conn) *WSConn {
c := &WSConn{
Conn: conn,
}
return c
}
func (conn *WSConn) Read(b []byte) (n int, err error) {
if len(conn.rb) == 0 {
_, conn.rb, err = conn.ReadMessage()
}
n = copy(b, conn.rb)
conn.rb = conn.rb[n:]
//log.Println("ws r:", n)
return
}
func (conn *WSConn) Write(b []byte) (n int, err error) {
err = conn.WriteMessage(websocket.BinaryMessage, b)
n = len(b)
//log.Println("ws w:", n)
return
}
func (conn *WSConn) SetDeadline(t time.Time) error {
if err := conn.SetReadDeadline(t); err != nil {
return err
}
return conn.SetWriteDeadline(t)
}
type WSServer struct {
Addr string
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 8192,
WriteBufferSize: 8192,
CheckOrigin: func(r *http.Request) bool { return true },
}
func (s *WSServer) handle(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
//defer conn.Close()
c := gosocks5.ServerConn(NewWSConn(conn), serverConfig)
/*
if err := c.Handleshake(); err != nil {
log.Println(err)
return
}
*/
serveSocks5(c)
}
func (s *WSServer) ListenAndServe() error {
http.HandleFunc("/", s.handle)
return http.ListenAndServe(s.Addr, nil)
}