forked from realDragonium/Ultraviolet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
89 lines (72 loc) · 1.73 KB
/
proxy.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
83
84
85
86
87
88
89
package ultraviolet
import (
"errors"
"log"
"net"
"github.com/realDragonium/Ultraviolet/config"
"github.com/realDragonium/Ultraviolet/core"
)
var (
maxConnParallelProcessing = make(chan struct{}, 100_000)
)
func NewProxy(uvReader config.UVConfigReader, l net.Listener, cfgReader config.ServerConfigReader) core.Proxy {
return &SpeedyProxy{
uvReader: uvReader,
listener: l,
cfgReader: cfgReader,
}
}
type SpeedyProxy struct {
uvReader config.UVConfigReader
listener net.Listener
cfgReader config.ServerConfigReader
serverCatalog core.ServerCatalog
}
func (p *SpeedyProxy) Start() error {
p.ReloadServerCatalog()
for {
conn, err := p.listener.Accept()
if errors.Is(err, net.ErrClosed) {
log.Printf("net.Listener was closed, stopping with accepting calls")
break
} else if err != nil {
log.Printf("got error: %v", err)
continue
}
claimParallelRun()
go func(conn net.Conn, serverCatalog core.ServerCatalog) {
defer unclaimParallelRun()
FullRun(conn, serverCatalog)
}(conn, p.serverCatalog)
}
return nil
}
func claimParallelRun() {
maxConnParallelProcessing <- struct{}{}
}
func unclaimParallelRun() {
<-maxConnParallelProcessing
}
func (p *SpeedyProxy) ReloadServerCatalog() error {
cfg, err := p.uvReader()
if err != nil {
return err
}
servers := make(map[string]core.Server)
newCfgs, err := p.cfgReader()
if err != nil {
return err
}
for _, newCfg := range newCfgs {
apiCfg, err := config.ServerToAPIConfig(newCfg)
if err != nil {
return err
}
server := NewAPIServer(apiCfg)
for _, domain := range newCfg.Domains {
servers[domain] = server
}
}
p.serverCatalog = core.NewServerCatalog(servers, cfg.DefaultStatusPk(), cfg.VerifyConnectionPk())
return nil
}