Skip to content

Commit

Permalink
Moved ciphers to core and simplified import graph
Browse files Browse the repository at this point in the history
  • Loading branch information
riobard committed Feb 11, 2017
1 parent ef958a0 commit 3e9aaf3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ GoDoc at https://godoc.org/github.com/riobard/go-shadowsocks2/
## Install

```sh
go get github.com/riobard/go-shadowsocks2
go get -u -v github.com/riobard/go-shadowsocks2
```


Expand Down
29 changes: 21 additions & 8 deletions cipher.go → core/cipher.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package core

import (
"crypto/md5"
Expand All @@ -7,12 +7,25 @@ import (
"sort"
"strings"

"github.com/riobard/go-shadowsocks2/core"
"github.com/riobard/go-shadowsocks2/shadowaead"
"github.com/riobard/go-shadowsocks2/shadowstream"
)

var errCipherNotSupported = errors.New("ciper not supported")
type Cipher interface {
StreamConnCipher
PacketConnCipher
}

type StreamConnCipher interface {
StreamConn(net.Conn) net.Conn
}

type PacketConnCipher interface {
PacketConn(net.PacketConn) net.PacketConn
}

// ErrCipherNotSupported occurs when a cipher is not supported (likely because of security concerns).
var ErrCipherNotSupported = errors.New("cipher not supported")

// List of AEAD ciphers: key size in bytes and constructor
var aeadList = map[string]struct {
Expand All @@ -39,8 +52,8 @@ var streamList = map[string]struct {
"chacha20-ietf": {32, shadowstream.Chacha20IETF},
}

// listCipher returns a list of available cipher names sorted alphabetically.
func listCipher() []string {
// ListCipher returns a list of available cipher names sorted alphabetically.
func ListCipher() []string {
var l []string
for k := range aeadList {
l = append(l, k)
Expand All @@ -52,8 +65,8 @@ func listCipher() []string {
return l
}

// derive key from password if given key is empty
func pickCipher(name string, key []byte, password string) (core.Cipher, error) {
// PickCipher returns a Cipher of the given name. Derive key from password if given key is empty.
func PickCipher(name string, key []byte, password string) (Cipher, error) {
name = strings.ToLower(name)

if name == "dummy" {
Expand Down Expand Up @@ -82,7 +95,7 @@ func pickCipher(name string, key []byte, password string) (core.Cipher, error) {
return &streamCipher{ciph}, err
}

return nil, errCipherNotSupported
return nil, ErrCipherNotSupported
}

type aeadCipher struct{ shadowaead.Cipher }
Expand Down
19 changes: 1 addition & 18 deletions core/doc.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
// Package core provides essential interfaces for Shadowsocks
// Package core implements essential parts of Shadowsocks
package core

import (
"net"
)

type Cipher interface {
StreamConnCipher
PacketConnCipher
}

type StreamConnCipher interface {
StreamConn(net.Conn) net.Conn
}

type PacketConnCipher interface {
PacketConn(net.PacketConn) net.PacketConn
}
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strings"
"syscall"
"time"

"github.com/riobard/go-shadowsocks2/core"
)

var config struct {
Expand Down Expand Up @@ -42,7 +44,7 @@ func main() {
}

flag.BoolVar(&config.Verbose, "verbose", false, "verbose mode")
flag.StringVar(&flags.Cipher, "cipher", "chacha20-ietf-poly1305", "available ciphers: "+strings.Join(listCipher(), " "))
flag.StringVar(&flags.Cipher, "cipher", "chacha20-ietf-poly1305", "available ciphers: "+strings.Join(core.ListCipher(), " "))
flag.StringVar(&flags.Key, "key", "", "base64url-encoded key (derive from password if empty)")
flag.IntVar(&flags.Keygen, "keygen", 0, "generate a base64url-encoded random key of given length in byte")
flag.StringVar(&flags.Password, "password", "", "password")
Expand Down Expand Up @@ -77,7 +79,7 @@ func main() {
key = k
}

ciph, err := pickCipher(flags.Cipher, key, flags.Password)
ciph, err := core.PickCipher(flags.Cipher, key, flags.Password)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 3e9aaf3

Please sign in to comment.