Skip to content

Commit

Permalink
Legacy client
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jun 13, 2022
1 parent d9b3192 commit 16b7653
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 153 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Some confusing protocol.
- [x] AEAD length chunk with padding
- [x] Stream chunk
- [x] AEAD Chunk
- [ ] Legacy client
- [x] Legacy client
- [x] AEAD Client
- [ ] Legacy server
- [ ] AEAD Server
8 changes: 6 additions & 2 deletions chunk_aead.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ type AEADReader struct {
}

func NewAEADReader(upstream io.Reader, cipher cipher.AEAD, nonce []byte) *AEADReader {
readNonce := make([]byte, cipher.NonceSize())
copy(readNonce, nonce)
return &AEADReader{
upstream: bufio.NewExtendedReader(upstream),
cipher: cipher,
nonce: nonce[:cipher.NonceSize()],
nonce: readNonce,
}
}

Expand Down Expand Up @@ -76,10 +78,12 @@ type AEADWriter struct {
}

func NewAEADWriter(upstream io.Writer, cipher cipher.AEAD, nonce []byte) *AEADWriter {
writeNonce := make([]byte, cipher.NonceSize())
copy(writeNonce, nonce)
return &AEADWriter{
upstream: bufio.NewExtendedWriter(upstream),
cipher: cipher,
nonce: nonce[:cipher.NonceSize()],
nonce: writeNonce,
}
}

Expand Down
8 changes: 6 additions & 2 deletions chunk_length_aead.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ type AEADChunkReader struct {
}

func NewAEADChunkReader(upstream io.Reader, cipher cipher.AEAD, nonce []byte, globalPadding sha3.ShakeHash) *AEADChunkReader {
readNonce := make([]byte, cipher.NonceSize())
copy(readNonce, nonce)
return &AEADChunkReader{
upstream: upstream,
cipher: cipher,
nonce: nonce[:cipher.NonceSize()],
nonce: readNonce,
globalPadding: globalPadding,
}
}
Expand Down Expand Up @@ -98,10 +100,12 @@ type AEADChunkWriter struct {
}

func NewAEADChunkWriter(upstream io.Writer, cipher cipher.AEAD, nonce []byte, globalPadding sha3.ShakeHash) *AEADChunkWriter {
writeNonce := make([]byte, cipher.NonceSize())
copy(writeNonce, nonce)
return &AEADChunkWriter{
upstream: upstream,
cipher: cipher,
nonce: nonce[:cipher.NonceSize()],
nonce: writeNonce,
globalPadding: globalPadding,
}
}
Expand Down
22 changes: 8 additions & 14 deletions chunk_stream.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package vmess

import (
"crypto/aes"
"crypto/cipher"
"io"

"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
N "github.com/sagernet/sing/common/network"
Expand All @@ -17,12 +15,10 @@ type StreamReader struct {
}

func NewStreamReader(upstream io.Reader, key []byte, iv []byte) *StreamReader {
var reader StreamReader
reader.upstream = bufio.NewExtendedReader(upstream)
block, err := aes.NewCipher(key)
common.Must(err)
reader.cipher = cipher.NewCFBDecrypter(block, iv)
return &reader
return &StreamReader{
upstream: bufio.NewExtendedReader(upstream),
cipher: newAesStream(key, iv, cipher.NewCFBDecrypter),
}
}

func (r *StreamReader) Read(p []byte) (n int, err error) {
Expand Down Expand Up @@ -53,12 +49,10 @@ type StreamWriter struct {
}

func NewStreamWriter(upstream io.Writer, key []byte, iv []byte) *StreamWriter {
var writer StreamWriter
writer.upstream = bufio.NewExtendedWriter(upstream)
block, err := aes.NewCipher(key)
common.Must(err)
writer.cipher = cipher.NewCFBEncrypter(block, iv)
return &writer
return &StreamWriter{
upstream: bufio.NewExtendedWriter(upstream),
cipher: newAesStream(key, iv, cipher.NewCFBEncrypter),
}
}

func (w *StreamWriter) Write(p []byte) (n int, err error) {
Expand Down
Loading

0 comments on commit 16b7653

Please sign in to comment.