forked from Kuingsmile/clash-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
65 lines (54 loc) · 1.13 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
55
56
57
58
59
60
61
62
63
64
65
package http
import (
"net"
"github.com/l552121229/clash-core-backup/common/cache"
C "github.com/l552121229/clash-core-backup/constant"
)
type Listener struct {
listener net.Listener
addr string
closed bool
}
// RawAddress implements C.Listener
func (l *Listener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
// Close implements C.Listener
func (l *Listener) Close() error {
l.closed = true
return l.listener.Close()
}
func New(addr string, in chan<- C.ConnContext) (C.Listener, error) {
return NewWithAuthenticate(addr, in, true)
}
func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool) (C.Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
var c *cache.LruCache
if authenticate {
c = cache.New(cache.WithAge(30))
}
hl := &Listener{
listener: l,
addr: addr,
}
go func() {
for {
conn, err := hl.listener.Accept()
if err != nil {
if hl.closed {
break
}
continue
}
go HandleConn(conn, in, c)
}
}()
return hl, nil
}