-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathserver.go
46 lines (40 loc) · 845 Bytes
/
server.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
package glutton
import (
"errors"
"fmt"
"net"
"github.com/seud0nym/tproxy-go/tproxy"
)
type Server struct {
tcpListener net.Listener
udpConn *net.UDPConn
tcpPort uint
udpPort uint
}
func NewServer(tcpPort, udpPort uint) *Server {
s := &Server{
tcpPort: tcpPort,
udpPort: udpPort,
}
return s
}
func (s *Server) Start() error {
tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%d", s.tcpPort))
if err != nil {
return err
}
if s.tcpListener, err = tproxy.ListenTCP("tcp4", tcpAddr); err != nil {
return err
}
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", s.udpPort))
if err != nil {
return err
}
if s.udpConn, err = tproxy.ListenUDP("udp4", udpAddr); err != nil {
return err
}
if s.udpConn == nil {
return errors.New("nil udp listener")
}
return nil
}