Skip to content

Commit

Permalink
Use a custom type for listener type.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Jun 14, 2024
1 parent 51a13a7 commit f8d7aa5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
12 changes: 8 additions & 4 deletions cmd/outline-ss-server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ type Service struct {
Keys []Key
}

type ListenerType string

const listenerTypeDirect ListenerType = "direct"

type Listener struct {
Type string
Type ListenerType
Address string
}

Expand Down Expand Up @@ -79,8 +83,8 @@ func ReadConfig(filename string) (*Config, error) {
for port, keys := range ports {
s := Service{
Listeners: []Listener{
Listener{Type: "direct", Address: fmt.Sprintf("tcp://[::]:%d", port)},
Listener{Type: "direct", Address: fmt.Sprintf("udp://[::]:%d", port)},
Listener{Type: listenerTypeDirect, Address: fmt.Sprintf("tcp://[::]:%d", port)},
Listener{Type: listenerTypeDirect, Address: fmt.Sprintf("udp://[::]:%d", port)},
},
Keys: keys,
}
Expand Down Expand Up @@ -111,7 +115,7 @@ func validateListener(u *url.URL) error {
return nil
}

func NewListener(addr string) (io.Closer, error) {
func newListener(addr string) (io.Closer, error) {
u, err := url.Parse(addr)
if err != nil {
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions cmd/outline-ss-server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestReadConfig(t *testing.T) {
Services: []Service{
Service{
Listeners: []Listener{
Listener{Type: "direct", Address: "tcp://[::]:9000"},
Listener{Type: "direct", Address: "udp://[::]:9000"},
Listener{Type: listenerTypeDirect, Address: "tcp://[::]:9000"},
Listener{Type: listenerTypeDirect, Address: "udp://[::]:9000"},
},
Keys: []Key{
Key{"user-0", "chacha20-ietf-poly1305", "Secret0"},
Expand All @@ -39,8 +39,8 @@ func TestReadConfig(t *testing.T) {
},
Service{
Listeners: []Listener{
Listener{Type: "direct", Address: "tcp://[::]:9001"},
Listener{Type: "direct", Address: "udp://[::]:9001"},
Listener{Type: listenerTypeDirect, Address: "tcp://[::]:9001"},
Listener{Type: listenerTypeDirect, Address: "udp://[::]:9001"},
},
Keys: []Key{
Key{"user-2", "chacha20-ietf-poly1305", "Secret2"},
Expand All @@ -59,8 +59,8 @@ func TestReadConfigParsesDeprecatedFormat(t *testing.T) {
Services: []Service{
Service{
Listeners: []Listener{
Listener{Type: "direct", Address: "tcp://[::]:9000"},
Listener{Type: "direct", Address: "udp://[::]:9000"},
Listener{Type: listenerTypeDirect, Address: "tcp://[::]:9000"},
Listener{Type: listenerTypeDirect, Address: "udp://[::]:9000"},
},
Keys: []Key{
Key{"user-0", "chacha20-ietf-poly1305", "Secret0"},
Expand All @@ -69,8 +69,8 @@ func TestReadConfigParsesDeprecatedFormat(t *testing.T) {
},
Service{
Listeners: []Listener{
Listener{Type: "direct", Address: "tcp://[::]:9001"},
Listener{Type: "direct", Address: "udp://[::]:9001"},
Listener{Type: listenerTypeDirect, Address: "tcp://[::]:9001"},
Listener{Type: listenerTypeDirect, Address: "udp://[::]:9001"},
},
Keys: []Key{
Key{"user-2", "chacha20-ietf-poly1305", "Secret2"},
Expand Down
8 changes: 3 additions & 5 deletions cmd/outline-ss-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const tcpReadTimeout time.Duration = 59 * time.Second
// A UDP NAT timeout of at least 5 minutes is recommended in RFC 4787 Section 4.3.
const defaultNatTimeout time.Duration = 5 * time.Minute

var directListenerType = "direct"

func init() {
var prefix = "%{level:.1s}%{time:2006-01-02T15:04:05.000Z07:00} %{pid} %{shortfile}]"
if term.IsTerminal(int(os.Stderr.Fd())) {
Expand Down Expand Up @@ -98,7 +96,7 @@ func (s *SSServer) serve(listener io.Closer, cipherList service.CipherList) erro
}

func (s *SSServer) start(addr string, cipherList service.CipherList) (io.Closer, error) {
listener, err := NewListener(addr)
listener, err := newListener(addr)
if err != nil {
//lint:ignore ST1005 Shadowsocks is capitalized.
return nil, fmt.Errorf("Shadowsocks service failed to start on address %v: %w", addr, err)
Expand All @@ -107,7 +105,7 @@ func (s *SSServer) start(addr string, cipherList service.CipherList) (io.Closer,

err = s.serve(listener, cipherList)
if err != nil {
return nil, fmt.Errorf("failed to serve on listener %w: %w", listener, err)
return nil, fmt.Errorf("failed to serve on listener %v: %w", listener, err)
}

return listener, nil
Expand Down Expand Up @@ -168,7 +166,7 @@ func (s *SSServer) loadConfig(filename string) error {
for _, listener := range serviceConfig.Listeners {
switch t := listener.Type; t {
// TODO: Support more listener types.
case directListenerType:
case listenerTypeDirect:
addrChanges[listener.Address] = 1
addrCiphers[listener.Address] = ciphers
default:
Expand Down

0 comments on commit f8d7aa5

Please sign in to comment.