MCPcopy
hub / github.com/txthinking/brook / NewSimpleStreamServer

Function NewSimpleStreamServer

simplestreamserver.go:39–86  ·  view source on GitHub ↗
(password []byte, src string, client net.Conn, timeout, udptimeout int)

Source from the content-addressed store, hash-verified

37}
38
39func NewSimpleStreamServer(password []byte, src string, client net.Conn, timeout, udptimeout int) (Exchanger, error) {
40 if timeout != 0 {
41 if err := client.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil {
42 return nil, err
43 }
44 }
45 s := &SimpleStreamServer{Client: client, Timeout: timeout, src: src}
46 b := x.BP2048.Get().([]byte)
47 if _, err := io.ReadFull(s.Client, b[:32+2]); err != nil {
48 x.BP2048.Put(b)
49 return nil, err
50 }
51 if bytes.Compare(password, b[:32]) != 0 {
52 x.BP2048.Put(b)
53 WaitReadErr(s.Client)
54 return nil, errors.New("Password is wrong")
55 }
56 l := int(binary.BigEndian.Uint16(b[32:34]))
57 if l > 2048 {
58 x.BP2048.Put(b)
59 return nil, errors.New("data too long")
60 }
61 if _, err := io.ReadFull(s.Client, b[:l]); err != nil {
62 x.BP2048.Put(b)
63 return nil, err
64 }
65 i := int64(binary.BigEndian.Uint32(b[:4]))
66 if time.Now().Unix()-i > 60 {
67 x.BP2048.Put(b)
68 WaitReadErr(s.Client)
69 return nil, errors.New("Expired request")
70 }
71 if i%2 == 0 {
72 s.network = "tcp"
73 s.RB = b
74 s.WB = x.BP2048.Get().([]byte)
75 }
76 if i%2 == 1 {
77 s.network = "udp"
78 s.Timeout = udptimeout
79 s.RB = x.BP65507.Get().([]byte)
80 copy(s.RB[:l], b[:l])
81 x.BP2048.Put(b)
82 s.WB = x.BP65507.Get().([]byte)
83 }
84 s.dst = socks5.ToAddress(s.RB[4], s.RB[4+1:l-2], s.RB[l-2:])
85 return ServerGate(s)
86}
87
88func (s *SimpleStreamServer) Exchange(remote net.Conn) error {
89 go func() {

Callers 2

ServeHTTPMethod · 0.85
ListenAndServeMethod · 0.85

Calls 3

WaitReadErrFunction · 0.85
GetMethod · 0.80
SetDeadlineMethod · 0.45

Tested by

no test coverage detected