Skip to content

Commit

Permalink
Move old config parsing to loadConfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Jun 14, 2024
1 parent 6e2068d commit 7114434
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
23 changes: 0 additions & 23 deletions cmd/outline-ss-server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,6 @@ func ReadConfig(filename string) (*Config, error) {
if err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}

// Specifying keys in `config.Keys` is a deprecated config format. We need to
// transform it to to the new format.
ports := make(map[int][]Key)
for _, keyConfig := range config.Keys {
ports[keyConfig.Port] = append(ports[keyConfig.Port], Key{
ID: keyConfig.ID,
Cipher: keyConfig.Cipher,
Secret: keyConfig.Secret,
})
}
for port, keys := range ports {
s := Service{
Listeners: []Listener{
Listener{Type: listenerTypeDirect, Address: fmt.Sprintf("tcp://[::]:%d", port)},
Listener{Type: listenerTypeDirect, Address: fmt.Sprintf("udp://[::]:%d", port)},
},
Keys: keys,
}
config.Services = append(config.Services, s)
}
config.Keys = nil

return &config, nil
}

Expand Down
31 changes: 12 additions & 19 deletions cmd/outline-ss-server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,22 @@ func TestReadConfigParsesDeprecatedFormat(t *testing.T) {

require.NoError(t, err)
expected := Config{
Services: []Service{
Service{
Listeners: []Listener{
Listener{Type: listenerTypeDirect, Address: "tcp://[::]:9000"},
Listener{Type: listenerTypeDirect, Address: "udp://[::]:9000"},
},
Keys: []Key{
Key{"user-0", "chacha20-ietf-poly1305", "Secret0"},
Key{"user-1", "chacha20-ietf-poly1305", "Secret1"},
},
Keys: []LegacyKeyService{
LegacyKeyService{
Key: Key{ID: "user-0", Cipher: "chacha20-ietf-poly1305", Secret: "Secret0"},
Port: 9000,
},
Service{
Listeners: []Listener{
Listener{Type: listenerTypeDirect, Address: "tcp://[::]:9001"},
Listener{Type: listenerTypeDirect, Address: "udp://[::]:9001"},
},
Keys: []Key{
Key{"user-2", "chacha20-ietf-poly1305", "Secret2"},
},
LegacyKeyService{
Key: Key{ID: "user-1", Cipher: "chacha20-ietf-poly1305", Secret: "Secret1"},
Port: 9000,
},
LegacyKeyService{
Key: Key{ID: "user-2", Cipher: "chacha20-ietf-poly1305", Secret: "Secret2"},
Port: 9001,
},
},
}
require.ElementsMatch(t, expected.Services, config.Services)
require.Equal(t, expected, *config)
}

func TestReadConfigFromEmptyFile(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions cmd/outline-ss-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ func (s *SSServer) loadConfig(filename string) error {
uniqueCiphers := 0
addrChanges := make(map[string]int)
addrCiphers := make(map[string]*list.List) // Values are *List of *CipherEntry.

for _, legacyKeyConfig := range config.Keys {
cryptoKey, err := shadowsocks.NewEncryptionKey(legacyKeyConfig.Cipher, legacyKeyConfig.Secret)
if err != nil {
return fmt.Errorf("failed to create encyption key for key %v: %w", legacyKeyConfig.ID, err)
}
entry := service.MakeCipherEntry(legacyKeyConfig.ID, cryptoKey, legacyKeyConfig.Secret)
for _, ln := range []string{"tcp", "udp"} {
addr := fmt.Sprintf("%s://[::]:%d", ln, legacyKeyConfig.Port)
addrChanges[addr] = 1
ciphers, ok := addrCiphers[addr]
if !ok {
ciphers = list.New()
addrCiphers[addr] = ciphers
}
ciphers.PushBack(&entry)
}
uniqueCiphers += 1
}

for _, serviceConfig := range config.Services {
if serviceConfig.Listeners == nil || serviceConfig.Keys == nil {
return fmt.Errorf("must specify at least 1 listener and 1 key per service")
Expand Down

0 comments on commit 7114434

Please sign in to comment.