Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: modularize UDP connection handling #188

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/outline-ss-server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ func (m *outlineMetrics) AddClosedTCPConnection(clientInfo ipinfo.IPInfo, client
}
}

func (m *outlineMetrics) AddUDPPacketFromClient(clientInfo ipinfo.IPInfo, accessKey, status string, clientProxyBytes, proxyTargetBytes int) {
func (m *outlineMetrics) AddUDPPacketFromClient(clientInfo ipinfo.IPInfo, accessKey, status string, data metrics.ProxyMetrics) {
m.udpPacketsFromClientPerLocation.WithLabelValues(clientInfo.CountryCode.String(), asnLabel(clientInfo.ASN), status).Inc()
addIfNonZero(int64(clientProxyBytes), m.dataBytes, "c>p", "udp", accessKey)
addIfNonZero(int64(clientProxyBytes), m.dataBytesPerLocation, "c>p", "udp", clientInfo.CountryCode.String(), asnLabel(clientInfo.ASN))
addIfNonZero(int64(proxyTargetBytes), m.dataBytes, "p>t", "udp", accessKey)
addIfNonZero(int64(proxyTargetBytes), m.dataBytesPerLocation, "p>t", "udp", clientInfo.CountryCode.String(), asnLabel(clientInfo.ASN))
addIfNonZero(data.ClientProxy, m.dataBytes, "c>p", "udp", accessKey)
addIfNonZero(data.ClientProxy, m.dataBytesPerLocation, "c>p", "udp", clientInfo.CountryCode.String(), asnLabel(clientInfo.ASN))
addIfNonZero(data.ProxyTarget, m.dataBytes, "p>t", "udp", accessKey)
addIfNonZero(data.ProxyTarget, m.dataBytesPerLocation, "p>t", "udp", clientInfo.CountryCode.String(), asnLabel(clientInfo.ASN))
}

func (m *outlineMetrics) AddUDPPacketFromTarget(clientInfo ipinfo.IPInfo, accessKey, status string, targetProxyBytes, proxyClientBytes int) {
Expand Down
23 changes: 17 additions & 6 deletions cmd/outline-ss-server/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,29 @@ func init() {

func TestMethodsDontPanic(t *testing.T) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewPedanticRegistry())
proxyMetrics := metrics.ProxyMetrics{
tcpProxyMetrics := metrics.ProxyMetrics{
ClientProxy: 1,
ProxyTarget: 2,
TargetProxy: 3,
ProxyClient: 4,
}
udpProxyMetrics := metrics.ProxyMetrics{
ClientProxy: 10,
ProxyTarget: 20,
TargetProxy: 30,
ProxyClient: 40,
}
ipInfo := ipinfo.IPInfo{CountryCode: "US", ASN: 100}
ssMetrics.SetBuildInfo("0.0.0-test")
ssMetrics.SetNumAccessKeys(20, 2)
ssMetrics.AddOpenTCPConnection(ipInfo)
ssMetrics.AddAuthenticatedTCPConnection(fakeAddr("127.0.0.1:9"), "0")
ssMetrics.AddClosedTCPConnection(ipInfo, fakeAddr("127.0.0.1:9"), "1", "OK", proxyMetrics, 10*time.Millisecond)
ssMetrics.AddUDPPacketFromClient(ipInfo, "2", "OK", 10, 20)
ssMetrics.AddClosedTCPConnection(ipInfo, fakeAddr("127.0.0.1:9"), "1", "OK", tcpProxyMetrics, 10*time.Millisecond)
ssMetrics.AddUDPPacketFromClient(ipInfo, "2", "OK", udpProxyMetrics)
ssMetrics.AddUDPPacketFromTarget(ipInfo, "3", "OK", 10, 20)
ssMetrics.AddUDPNatEntry(fakeAddr("127.0.0.1:9"), "key-1")
ssMetrics.RemoveUDPNatEntry(fakeAddr("127.0.0.1:9"), "key-1")
ssMetrics.AddTCPProbe("ERR_CIPHER", "eof", 443, proxyMetrics.ClientProxy)
ssMetrics.AddTCPProbe("ERR_CIPHER", "eof", 443, tcpProxyMetrics.ClientProxy)
ssMetrics.AddTCPCipherSearch(true, 10*time.Millisecond)
ssMetrics.AddUDPCipherSearch(true, 10*time.Millisecond)
}
Expand Down Expand Up @@ -174,14 +180,19 @@ func BenchmarkProbe(b *testing.B) {

func BenchmarkClientUDP(b *testing.B) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry())
proxyMetrics := metrics.ProxyMetrics{
ClientProxy: 1000,
ProxyTarget: 2000,
TargetProxy: 3000,
ProxyClient: 4000,
}
clientInfo := ipinfo.IPInfo{CountryCode: "ZZ", ASN: 100}
accessKey := "key 1"
status := "OK"
size := 1000
timeToCipher := time.Microsecond
b.ResetTimer()
for i := 0; i < b.N; i++ {
ssMetrics.AddUDPPacketFromClient(clientInfo, accessKey, status, size, size)
ssMetrics.AddUDPPacketFromClient(clientInfo, accessKey, status, proxyMetrics)
ssMetrics.AddUDPCipherSearch(true, timeToCipher)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ var _ service.UDPMetrics = (*fakeUDPMetrics)(nil)
func (m *fakeUDPMetrics) GetIPInfo(ip net.IP) (ipinfo.IPInfo, error) {
return ipinfo.IPInfo{CountryCode: "QQ"}, nil
}
func (m *fakeUDPMetrics) AddUDPPacketFromClient(clientInfo ipinfo.IPInfo, accessKey, status string, clientProxyBytes, proxyTargetBytes int) {
m.up = append(m.up, udpRecord{clientInfo, accessKey, status, clientProxyBytes, proxyTargetBytes})
func (m *fakeUDPMetrics) AddUDPPacketFromClient(clientInfo ipinfo.IPInfo, accessKey, status string, data metrics.ProxyMetrics) {
m.up = append(m.up, udpRecord{clientInfo, accessKey, status, int(data.ClientProxy), int(data.ProxyTarget)})
}
func (m *fakeUDPMetrics) AddUDPPacketFromTarget(clientInfo ipinfo.IPInfo, accessKey, status string, targetProxyBytes, proxyClientBytes int) {
m.down = append(m.down, udpRecord{clientInfo, accessKey, status, targetProxyBytes, proxyClientBytes})
Expand Down
Loading
Loading