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: move outline-ss-server to cmd/ and hide code #136

Merged
merged 8 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

# Git submodules
/third_party/maxmind/

# Prometheus
/data/
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ project_name: outline-ss-server
# Skip 32 bit macOS builds.
builds:
-
main: ./cmd/outline-ss-server/main.go
env:
- CGO_ENABLED=0
goos:
Expand Down
11 changes: 3 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
BUILDDIR=$(CURDIR)/dist
GOBIN=$(CURDIR)/bin
GORELEASER=$(GOBIN)/goreleaser
GORELEASER=go run github.com/goreleaser/goreleaser

.PHONY: release release-local test clean clean-all

# This requires GITHUB_TOKEN to be set.
release: clean-all $(GORELEASER)
release: clean-all
$(GORELEASER)

release-local: $(GORELEASER)
release-local:
$(GORELEASER) --rm-dist --snapshot

test: third_party/maxmind/test-data/GeoIP2-Country-Test.mmdb
Expand All @@ -17,9 +16,6 @@ test: third_party/maxmind/test-data/GeoIP2-Country-Test.mmdb
third_party/maxmind/test-data/GeoIP2-Country-Test.mmdb:
git submodule update --init

$(GORELEASER): go.mod
env GOBIN=$(GOBIN) go install github.com/goreleaser/goreleaser

go.mod: tools.go
go mod tidy
touch go.mod
Expand All @@ -30,4 +26,3 @@ clean:

clean-all: clean
rm -rf $(CURDIR)/third_party/maxmind/*
rm -rf $(GOBIN)
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ The Outline Shadowsocks service allows for:

Fetch dependencies for this demo:
```
GO111MODULE=off go get github.com/shadowsocks/go-shadowsocks2 github.com/prometheus/prometheus/cmd/...
GO111MODULE=off go get github.com/prometheus/prometheus/cmd/...
```
If that doesn't work, download the [prometheus](https://prometheus.io/download/) or [go-shadowsocks2](https://github.com/shadowsocks/go-shadowsocks2/releases) binaries directly.
If that doesn't work, download the [prometheus](https://prometheus.io/download/) binary directly.


### Run the server
On Terminal 1, from the repository directory, build and start the SS server:
```
go run . -config config_example.yml -metrics localhost:9091 --replay_history=10000
go run ./cmd/outline-ss-server -config cmd/outline-ss-server/config_example.yml -metrics localhost:9091 --replay_history=10000
```
In production, you may want to specify `-ip_country_db` to get per-country metrics. See [how the Outline Server calls outline-ss-server](https://github.com/Jigsaw-Code/outline-server/blob/master/src/shadowbox/server/outline_shadowsocks_server.ts).


### Run the Prometheus scraper for metrics collection
On Terminal 2, start prometheus scraper for metrics collection:
```
$(go env GOPATH)/bin/prometheus --config.file=prometheus_example.yml
$(go env GOPATH)/bin/prometheus --config.file=cmd/outline-ss-server/prometheus_example.yml
```

### Run the SOCKS-to-Shadowsocks client
On Terminal 3, start the SS client:
```
$(go env GOPATH)/bin/go-shadowsocks2 -c ss://chacha20-ietf-poly1305:Secret0@:9000 -verbose -socks localhost:1080
go run github.com/shadowsocks/go-shadowsocks2 -c ss://chacha20-ietf-poly1305:Secret0@:9000 -verbose -socks localhost:1080
```

### Fetch a page over Shadowsocks
Expand All @@ -70,12 +70,12 @@ iperf3 -s

Start the SS server (listening on port 9000):
```
go run . -config config_example.yml
go run ./cmd/outline-ss-server -config cmd/outline-ss-server/config_example.yml
```

Start the SS tunnel to redirect port 8000 -> localhost:5201 via the proxy on 9000:
```
$(go env GOPATH)/bin/go-shadowsocks2 -c ss://chacha20-ietf-poly1305:Secret0@:9000 -tcptun ":8000=localhost:5201" -udptun ":8000=localhost:5201" -verbose
go run github.com/shadowsocks/go-shadowsocks2 -c ss://chacha20-ietf-poly1305:Secret0@:9000 -tcptun ":8000=localhost:5201" -udptun ":8000=localhost:5201" -verbose
```

Test TCP upload (client -> server):
Expand All @@ -102,7 +102,7 @@ iperf3 -c localhost -p 8000 --udp -b 0 --reverse

Run the commands above, but start the SS server with
```
$(go env GOPATH)/bin/go-shadowsocks2 -s ss://chacha20-ietf-poly1305:Secret0@:9000 -verbose
go run github.com/shadowsocks/go-shadowsocks2 -s ss://chacha20-ietf-poly1305:Secret0@:9000 -verbose
```


Expand Down
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"net"
"time"

"github.com/Jigsaw-Code/outline-ss-server/internal/slicepool"
onet "github.com/Jigsaw-Code/outline-ss-server/net"
ss "github.com/Jigsaw-Code/outline-ss-server/shadowsocks"
"github.com/Jigsaw-Code/outline-ss-server/slicepool"
"github.com/shadowsocks/go-shadowsocks2/socks"
)

Expand Down Expand Up @@ -156,7 +156,7 @@ func (c *packetConn) ReadFrom(b []byte) (int, net.Addr, error) {
if socksSrcAddr == nil {
return 0, nil, errors.New("Failed to read source address")
}
srcAddr := NewAddr(socksSrcAddr.String(), "udp")
srcAddr := newAddr(socksSrcAddr.String(), "udp")
n = copy(b, buf[len(socksSrcAddr):]) // Strip the SOCKS source address
if len(b) < len(buf)-len(socksSrcAddr) {
return n, srcAddr, io.ErrShortBuffer
Expand All @@ -177,8 +177,8 @@ func (a *addr) Network() string {
return a.network
}

// NewAddr returns a net.Addr that holds an address of the form `host:port` with a domain name or IP as host.
// newAddr returns a net.Addr that holds an address of the form `host:port` with a domain name or IP as host.
// Used for SOCKS addressing.
func NewAddr(address, network string) net.Addr {
func newAddr(address, network string) net.Addr {
return &addr{address: address, network: network}
}
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestShadowsocksClient_ListenUDP(t *testing.T) {
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
pcrw := &packetConnReadWriter{PacketConn: conn, targetAddr: NewAddr(testTargetAddr, "udp")}
pcrw := &packetConnReadWriter{PacketConn: conn, targetAddr: newAddr(testTargetAddr, "udp")}
expectEchoPayload(pcrw, ss.MakeTestPayload(1024), make([]byte, 1024), t)

proxy.Close()
Expand Down Expand Up @@ -236,7 +236,7 @@ func BenchmarkShadowsocksClient_ListenUDP(b *testing.B) {
buf := make([]byte, clientUDPBufferSize)
for n := 0; n < b.N; n++ {
payload := ss.MakeTestPayload(1024)
pcrw := &packetConnReadWriter{PacketConn: conn, targetAddr: NewAddr(testTargetAddr, "udp")}
pcrw := &packetConnReadWriter{PacketConn: conn, targetAddr: newAddr(testTargetAddr, "udp")}
b.StartTimer()
expectEchoPayload(pcrw, payload, buf, b)
b.StopTimer()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion service/cipher_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type CipherEntry struct {
// MakeCipherEntry constructs a CipherEntry.
func MakeCipherEntry(id string, cipher *ss.Cipher, secret string) CipherEntry {
var saltGenerator ServerSaltGenerator
if cipher.SaltSize()-ServerSaltMarkLen >= minSaltEntropy {
if cipher.SaltSize()-serverSaltMarkLen >= minSaltEntropy {
// Mark salts with a tag for reverse replay protection.
saltGenerator = NewServerSaltGenerator(secret)
} else {
Expand Down
10 changes: 5 additions & 5 deletions service/server_salt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ type serverSaltGenerator struct {
key []byte
}

// ServerSaltMarkLen is the number of bytes of salt to use as a marker.
// serverSaltMarkLen is the number of bytes of salt to use as a marker.
// Increasing this value reduces the false positive rate, but increases
// the likelihood of salt collisions.
const ServerSaltMarkLen = 4 // Must be less than or equal to SHA1.Size()
const serverSaltMarkLen = 4 // Must be less than or equal to SHA1.Size()

// Constant to identify this marking scheme.
var serverSaltLabel = []byte("outline-server-salt")
Expand All @@ -79,9 +79,9 @@ func NewServerSaltGenerator(secret string) ServerSaltGenerator {
}

func (sg serverSaltGenerator) splitSalt(salt []byte) (prefix, mark []byte, err error) {
prefixLen := len(salt) - ServerSaltMarkLen
prefixLen := len(salt) - serverSaltMarkLen
if prefixLen < 0 {
return nil, nil, fmt.Errorf("Salt is too short: %d < %d", len(salt), ServerSaltMarkLen)
return nil, nil, fmt.Errorf("Salt is too short: %d < %d", len(salt), serverSaltMarkLen)
}
return salt[:prefixLen], salt[prefixLen:], nil
}
Expand Down Expand Up @@ -116,5 +116,5 @@ func (sg serverSaltGenerator) IsServerSalt(salt []byte) bool {
return false
}
tag := sg.getTag(prefix)
return bytes.Equal(tag[:ServerSaltMarkLen], mark)
return bytes.Equal(tag[:serverSaltMarkLen], mark)
}
2 changes: 1 addition & 1 deletion shadowsocks/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io"
"sync"

"github.com/Jigsaw-Code/outline-ss-server/slicepool"
"github.com/Jigsaw-Code/outline-ss-server/internal/slicepool"
)

// payloadSizeMask is the maximum size of payload in bytes.
Expand Down