-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.go
54 lines (47 loc) · 1.22 KB
/
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
47
48
49
50
51
52
53
54
package usocksd
import (
"fmt"
"net"
"strconv"
"github.com/cybozu-go/usocksd/metrics"
"github.com/cybozu-go/usocksd/socks"
)
// Listeners returns a list of net.Listener.
func Listeners(c *Config) ([]net.Listener, error) {
if len(c.Incoming.Addresses) == 0 {
ln, err := net.Listen("tcp", ":"+strconv.Itoa(c.Incoming.Port))
if err != nil {
return nil, err
}
return []net.Listener{ln}, nil
}
lns := make([]net.Listener, len(c.Incoming.Addresses))
for i, a := range c.Incoming.Addresses {
addr := net.JoinHostPort(a.String(), strconv.Itoa(c.Incoming.Port))
ln, err := net.Listen("tcp", addr)
if err != nil {
for j := 0; j < i; j++ {
lns[j].Close()
}
return nil, err
}
lns[i] = ln
}
return lns, nil
}
// MetricsListener returns a listener for the metrics server.
func MetricsListener(c *Config) (net.Listener, error) {
addr := fmt.Sprintf(":%d", c.Incoming.MetricsPort)
return net.Listen("tcp", addr)
}
// NewServer creates a new socks.Server.
func NewServer(c *Config) *socks.Server {
return &socks.Server{
Rules: createRuleSet(c),
Dialer: createDialer(c),
}
}
// NewMetricsServer creates a new metrics.Server.
func NewMetricsServer(_ *Config) *metrics.Server {
return &metrics.Server{}
}