Skip to content

Commit cc3fdb6

Browse files
committed
Version 1.0 alpha
1 parent f3a12e9 commit cc3fdb6

21 files changed

+277
-101
lines changed

io/aes.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func NewAesDecryptReader(key []byte, iv []byte, reader io.Reader) (io.Reader, er
1212
return nil, err
1313
}
1414

15-
aesMode := cipher.NewCBCDecrypter(aesBlock, iv)
16-
return NewCryptionReader(aesMode, reader), nil
15+
aesStream := cipher.NewCFBDecrypter(aesBlock, iv)
16+
return NewCryptionReader(aesStream, reader), nil
1717
}
1818

1919
func NewAesEncryptWriter(key []byte, iv []byte, writer io.Writer) (io.Writer, error) {
@@ -22,6 +22,6 @@ func NewAesEncryptWriter(key []byte, iv []byte, writer io.Writer) (io.Writer, er
2222
return nil, err
2323
}
2424

25-
aesMode := cipher.NewCBCEncrypter(aesBlock, iv)
26-
return NewCryptionWriter(aesMode, writer), nil
25+
aesStream := cipher.NewCFBEncrypter(aesBlock, iv)
26+
return NewCryptionWriter(aesStream, writer), nil
2727
}

io/encryption.go

+15-22
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ package io
33
import (
44
"crypto/cipher"
55
"io"
6+
7+
"github.com/v2ray/v2ray-core/log"
68
)
79

810
// CryptionReader is a general purpose reader that applies
911
// block cipher on top of a regular reader.
1012
type CryptionReader struct {
11-
mode cipher.BlockMode
13+
stream cipher.Stream
1214
reader io.Reader
1315
}
1416

15-
func NewCryptionReader(mode cipher.BlockMode, reader io.Reader) *CryptionReader {
17+
func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader {
1618
this := new(CryptionReader)
17-
this.mode = mode
19+
this.stream = stream
1820
this.reader = reader
1921
return this
2022
}
@@ -23,43 +25,34 @@ func NewCryptionReader(mode cipher.BlockMode, reader io.Reader) *CryptionReader
2325
// a multiply of BlockSize()
2426
func (reader CryptionReader) Read(blocks []byte) (int, error) {
2527
nBytes, err := reader.reader.Read(blocks)
26-
if err != nil && err != io.EOF {
27-
return nBytes, err
28+
log.Debug("CryptionReader: Read %d bytes", nBytes)
29+
if nBytes > 0 {
30+
reader.stream.XORKeyStream(blocks[:nBytes], blocks[:nBytes])
2831
}
29-
if nBytes < len(blocks) {
30-
for i, _ := range blocks[nBytes:] {
31-
blocks[i] = 0
32-
}
32+
if err != nil {
33+
log.Error("Error reading blocks: %v", err)
3334
}
34-
reader.mode.CryptBlocks(blocks, blocks)
3535
return nBytes, err
3636
}
3737

38-
func (reader CryptionReader) BlockSize() int {
39-
return reader.mode.BlockSize()
40-
}
41-
4238
// Cryption writer is a general purpose of byte stream writer that applies
4339
// block cipher on top of a regular writer.
4440
type CryptionWriter struct {
45-
mode cipher.BlockMode
41+
stream cipher.Stream
4642
writer io.Writer
4743
}
4844

49-
func NewCryptionWriter(mode cipher.BlockMode, writer io.Writer) *CryptionWriter {
45+
func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
5046
this := new(CryptionWriter)
51-
this.mode = mode
47+
this.stream = stream
5248
this.writer = writer
5349
return this
5450
}
5551

5652
// Write writes the give blocks to underlying writer. The length of the blocks
5753
// must be a multiply of BlockSize()
5854
func (writer CryptionWriter) Write(blocks []byte) (int, error) {
59-
writer.mode.CryptBlocks(blocks, blocks)
55+
log.Debug("CryptionWriter writing %d bytes", len(blocks))
56+
writer.stream.XORKeyStream(blocks, blocks)
6057
return writer.writer.Write(blocks)
6158
}
62-
63-
func (writer CryptionWriter) BlockSize() int {
64-
return writer.mode.BlockSize()
65-
}

io/vmess/decryptionreader.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func NewDecryptionReader(reader io.Reader, key []byte, iv []byte) (*DecryptionRe
2929
if err != nil {
3030
return nil, err
3131
}
32-
aesBlockMode := cipher.NewCBCDecrypter(aesCipher, iv)
33-
decryptionReader.reader = v2io.NewCryptionReader(aesBlockMode, reader)
32+
aesStream := cipher.NewCFBDecrypter(aesCipher, iv)
33+
decryptionReader.reader = v2io.NewCryptionReader(aesStream, reader)
3434
decryptionReader.buffer = bytes.NewBuffer(make([]byte, 0, 2*blockSize))
3535
return decryptionReader, nil
3636
}

io/vmess/decryptionreader_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func TestNormalReading(t *testing.T) {
3535
aesBlock, err := aes.NewCipher(key)
3636
assert.Error(err).IsNil()
3737

38-
aesMode := cipher.NewCBCEncrypter(aesBlock, iv)
38+
aesStream := cipher.NewCFBEncrypter(aesBlock, iv)
3939

4040
ciphertext := make([]byte, testSize)
41-
aesMode.CryptBlocks(ciphertext, plaintext)
41+
aesStream.XORKeyStream(ciphertext, plaintext)
4242

4343
ciphertextcopy := make([]byte, testSize)
4444
copy(ciphertextcopy, ciphertext)

io/vmess/vmess.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) erro
224224
if err != nil {
225225
return err
226226
}
227-
aesMode := cipher.NewCBCEncrypter(aesCipher, make([]byte, blockSize))
228-
cWriter := v2io.NewCryptionWriter(aesMode, writer)
227+
aesStream := cipher.NewCFBEncrypter(aesCipher, make([]byte, blockSize))
228+
cWriter := v2io.NewCryptionWriter(aesStream, writer)
229229

230230
_, err = writer.Write(buffer[0:encryptionBegin])
231231
if err != nil {

log/log.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,33 @@ func SetLogLevel(level LogLevel) {
2121
logLevel = level
2222
}
2323

24-
func writeLog(data string, level LogLevel) {
24+
func writeLog(level LogLevel, prefix, format string, v ...interface{}) string {
2525
if level < logLevel {
26-
return
26+
return ""
2727
}
28-
log.Print(data)
28+
var data string
29+
if v == nil || len(v) == 0 {
30+
data = format
31+
} else {
32+
data = fmt.Sprintf(format, v...)
33+
}
34+
log.Print(prefix + data)
35+
return data
2936
}
3037

3138
func Debug(format string, v ...interface{}) {
32-
data := fmt.Sprintf(format, v)
33-
writeLog("[Debug]"+data, DebugLevel)
39+
writeLog(DebugLevel, "[Debug]", format, v...)
3440
}
3541

3642
func Info(format string, v ...interface{}) {
37-
data := fmt.Sprintf(format, v)
38-
writeLog("[Info]"+data, InfoLevel)
43+
writeLog(InfoLevel, "[Info]", format, v...)
3944
}
4045

4146
func Warning(format string, v ...interface{}) {
42-
data := fmt.Sprintf(format, v)
43-
writeLog("[Warning]"+data, WarningLevel)
47+
writeLog(WarningLevel, "[Warning]", format, v...)
4448
}
4549

4650
func Error(format string, v ...interface{}) error {
47-
data := fmt.Sprintf(format, v)
48-
writeLog("[Error]"+data, ErrorLevel)
51+
data := writeLog(ErrorLevel, "[Error]", format, v...)
4952
return errors.New(data)
5053
}

net/freedom/freedom.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package freedom
22

33
import (
44
"io"
5-
"log"
65
"net"
76

87
"github.com/v2ray/v2ray-core"
8+
"github.com/v2ray/v2ray-core/log"
99
v2net "github.com/v2ray/v2ray-core/net"
1010
)
1111

@@ -24,10 +24,9 @@ func (vconn *VFreeConnection) Start(vRay core.OutboundVRay) error {
2424
output := vRay.OutboundOutput()
2525
conn, err := net.Dial("tcp", vconn.dest.String())
2626
if err != nil {
27-
log.Print(err)
28-
return err
27+
return log.Error("Failed to open tcp: %s", vconn.dest.String())
2928
}
30-
log.Print("Working on tcp:" + vconn.dest.String())
29+
log.Debug("Sending outbound tcp: %s", vconn.dest.String())
3130

3231
finish := make(chan bool, 2)
3332
go vconn.DumpInput(conn, input, finish)
@@ -41,22 +40,25 @@ func (vconn *VFreeConnection) DumpInput(conn net.Conn, input <-chan []byte, fini
4140
data, open := <-input
4241
if !open {
4342
finish <- true
43+
log.Debug("Freedom finishing input.")
4444
break
4545
}
46-
conn.Write(data)
46+
nBytes, err := conn.Write(data)
47+
log.Debug("Freedom wrote %d bytes with error %v", nBytes, err)
4748
}
4849
}
4950

5051
func (vconn *VFreeConnection) DumpOutput(conn net.Conn, output chan<- []byte, finish chan<- bool) {
5152
for {
52-
buffer := make([]byte, 128)
53+
buffer := make([]byte, 512)
5354
nBytes, err := conn.Read(buffer)
55+
log.Debug("Freedom reading %d bytes with error %v", nBytes, err)
5456
if err == io.EOF {
5557
close(output)
5658
finish <- true
59+
log.Debug("Freedom finishing output.")
5760
break
5861
}
59-
log.Print(buffer[:nBytes])
6062
output <- buffer[:nBytes]
6163
}
6264
}

net/freedom/freedomfactory.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ type FreedomFactory struct {
1111
func (factory FreedomFactory) Create(vp *core.VPoint, config []byte, dest v2net.VAddress) (core.OutboundConnectionHandler, error) {
1212
return NewVFreeConnection(dest), nil
1313
}
14+
15+
func init() {
16+
core.RegisterOutboundConnectionHandlerFactory("freedom", FreedomFactory{})
17+
}

net/socks/socks.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ func NewSocksServer(vp *core.VPoint, rawConfig []byte) *SocksServer {
3737
func (server *SocksServer) Listen(port uint16) error {
3838
listener, err := net.Listen("tcp", ":"+strconv.Itoa(int(port)))
3939
if err != nil {
40-
return log.Error("Error on listening port %d: %v", port, err)
40+
log.Error("Error on listening port %d: %v", port, err)
41+
return err
4142
}
43+
log.Debug("Working on tcp:%d", port)
4244
server.accepting = true
4345
go server.AcceptConnections(listener)
4446
return nil
@@ -48,7 +50,8 @@ func (server *SocksServer) AcceptConnections(listener net.Listener) error {
4850
for server.accepting {
4951
connection, err := listener.Accept()
5052
if err != nil {
51-
return log.Error("Error on accepting socks connection: %v", err)
53+
log.Error("Error on accepting socks connection: %v", err)
54+
return err
5255
}
5356
go server.HandleConnection(connection)
5457
}
@@ -60,7 +63,8 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
6063

6164
auth, err := socksio.ReadAuthentication(connection)
6265
if err != nil {
63-
return log.Error("Error on reading authentication: %v", err)
66+
log.Error("Error on reading authentication: %v", err)
67+
return err
6468
}
6569

6670
expectedAuthMethod := socksio.AuthNotRequired
@@ -76,12 +80,14 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
7680
return ErrorAuthenticationFailed
7781
}
7882

83+
log.Debug("Auth accepted, responding auth.")
7984
authResponse := socksio.NewAuthenticationResponse(socksio.AuthNotRequired)
8085
socksio.WriteAuthentication(connection, authResponse)
8186

8287
request, err := socksio.ReadRequest(connection)
8388
if err != nil {
84-
return log.Error("Error on reading socks request: %v", err)
89+
log.Error("Error on reading socks request: %v", err)
90+
return err
8591
}
8692

8793
response := socksio.NewSocks5Response()
@@ -105,6 +111,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
105111
case socksio.AddrTypeDomain:
106112
response.Domain = request.Domain
107113
}
114+
log.Debug("Socks response port = %d", response.Port)
108115
socksio.WriteResponse(connection, response)
109116

110117
ray := server.vPoint.NewInboundConnectionAccepted(request.Destination())
@@ -121,12 +128,13 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
121128

122129
func (server *SocksServer) dumpInput(conn net.Conn, input chan<- []byte, finish chan<- bool) {
123130
for {
124-
buffer := make([]byte, 256)
131+
buffer := make([]byte, 512)
125132
nBytes, err := conn.Read(buffer)
126133
log.Debug("Reading %d bytes, with error %v", nBytes, err)
127134
if err == io.EOF {
128135
close(input)
129136
finish <- true
137+
log.Debug("Socks finishing input.")
130138
break
131139
}
132140
input <- buffer[:nBytes]
@@ -138,10 +146,11 @@ func (server *SocksServer) dumpOutput(conn net.Conn, output <-chan []byte, finis
138146
buffer, open := <-output
139147
if !open {
140148
finish <- true
149+
log.Debug("Socks finishing output")
141150
break
142151
}
143-
nBytes, _ := conn.Write(buffer)
144-
log.Debug("Writing %d bytes", nBytes)
152+
nBytes, err := conn.Write(buffer)
153+
log.Debug("Writing %d bytes with error %v", nBytes, err)
145154
}
146155
}
147156

net/socks/socks_test.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ import (
1111

1212
func TestSocksTcpConnect(t *testing.T) {
1313
t.Skip("Not ready yet.")
14-
/*
15-
assert := unit.Assert(t)
14+
/*
15+
assert := unit.Assert(t)
1616
17-
port := uint16(12384)
17+
port := uint16(12384)
1818
19-
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
20-
vid, err := core.UUIDToVID(uuid)
21-
assert.Error(err).IsNil()
19+
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
20+
vid, err := core.UUIDToVID(uuid)
21+
assert.Error(err).IsNil()
2222
23-
config := core.VConfig{
24-
port,
25-
[]core.VUser{core.VUser{vid}},
26-
"",
27-
[]core.VNext{}}
23+
config := core.VConfig{
24+
port,
25+
[]core.VUser{core.VUser{vid}},
26+
"",
27+
[]core.VNext{}}
2828
29-
och := new(mocks.FakeOutboundConnectionHandler)
30-
och.Data2Send = bytes.NewBuffer(make([]byte, 1024))
31-
och.Data2Return = []byte("The data to be returned to socks server.")
29+
och := new(mocks.FakeOutboundConnectionHandler)
30+
och.Data2Send = bytes.NewBuffer(make([]byte, 1024))
31+
och.Data2Return = []byte("The data to be returned to socks server.")
3232
33-
vpoint, err := core.NewVPoint(&config, SocksServerFactory{}, och)
34-
assert.Error(err).IsNil()
33+
vpoint, err := core.NewVPoint(&config, SocksServerFactory{}, och)
34+
assert.Error(err).IsNil()
3535
36-
err = vpoint.Start()
37-
assert.Error(err).IsNil()
38-
*/
36+
err = vpoint.Start()
37+
assert.Error(err).IsNil()
38+
*/
3939
}

net/socks/socksfactory.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ type SocksServerFactory struct {
1010
func (factory SocksServerFactory) Create(vp *core.VPoint, config []byte) (core.InboundConnectionHandler, error) {
1111
return NewSocksServer(vp, config), nil
1212
}
13+
14+
func init() {
15+
core.RegisterInboundConnectionHandlerFactory("socks", SocksServerFactory{})
16+
}

0 commit comments

Comments
 (0)