Skip to content

Commit d12737b

Browse files
committedSep 13, 2015
Simplify channel operations
1 parent ef95077 commit d12737b

File tree

11 files changed

+63
-176
lines changed

11 files changed

+63
-176
lines changed
 

‎config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ConnectionConfig struct {
1616

1717
// Config is the config for Point server.
1818
type Config struct {
19-
Port uint16 `json:"port"` // Port of this Point server.
19+
Port uint16 `json:"port"` // Port of this Point server.
2020
InboundConfig ConnectionConfig `json:"inbound"`
2121
OutboundConfig ConnectionConfig `json:"outbound"`
2222
}

‎io/bufferset.go

-75
This file was deleted.

‎io/vmess/vmess.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12-
_ "log"
1312
mrand "math/rand"
1413

1514
"github.com/v2ray/v2ray-core"
@@ -27,6 +26,8 @@ const (
2726

2827
var (
2928
ErrorInvalidUser = errors.New("Invalid User")
29+
30+
emptyIV = make([]byte, blockSize)
3031
)
3132

3233
// VMessRequest implements the request message of VMess protocol. It only contains
@@ -75,7 +76,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
7576
}
7677
request.UserId = *userId
7778

78-
decryptor, err := NewDecryptionReader(reader, userId.Hash([]byte("PWD")), make([]byte, blockSize))
79+
decryptor, err := NewDecryptionReader(reader, userId.Hash([]byte("PWD")), emptyIV)
7980
if err != nil {
8081
return nil, err
8182
}
@@ -224,7 +225,7 @@ func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) erro
224225
if err != nil {
225226
return err
226227
}
227-
aesStream := cipher.NewCFBEncrypter(aesCipher, make([]byte, blockSize))
228+
aesStream := cipher.NewCFBEncrypter(aesCipher, emptyIV)
228229
cWriter := v2io.NewCryptionWriter(aesStream, writer)
229230

230231
_, err = writer.Write(buffer[0:encryptionBegin])

‎net/vdest.go ‎net/address.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package core
1+
package net
22

33
import (
44
"net"

‎net/freedom/freedom.go

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package freedom
22

33
import (
4-
"io"
54
"net"
65

76
"github.com/v2ray/v2ray-core"
@@ -36,31 +35,14 @@ func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
3635
}
3736

3837
func (vconn *FreedomConnection) DumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {
39-
for {
40-
data, open := <-input
41-
if !open {
42-
finish <- true
43-
log.Debug("Freedom finishing input.")
44-
break
45-
}
46-
nBytes, err := conn.Write(data)
47-
log.Debug("Freedom wrote %d bytes with error %v", nBytes, err)
48-
}
38+
v2net.ChanToWriter(conn, input)
39+
finish <- true
4940
}
5041

5142
func (vconn *FreedomConnection) DumpOutput(conn net.Conn, output chan<- []byte, finish chan<- bool) {
52-
for {
53-
buffer := make([]byte, 512)
54-
nBytes, err := conn.Read(buffer)
55-
log.Debug("Freedom reading %d bytes with error %v", nBytes, err)
56-
if err == io.EOF {
57-
close(output)
58-
finish <- true
59-
log.Debug("Freedom finishing output.")
60-
break
61-
}
62-
output <- buffer[:nBytes]
63-
}
43+
v2net.ReaderToChan(output, conn)
44+
close(output)
45+
finish <- true
6446
}
6547

6648
func (vconn *FreedomConnection) CloseConn(conn net.Conn, finish <-chan bool) {

‎net/socks/socks.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package socks
22

33
import (
44
"errors"
5-
"io"
65
"net"
76
"strconv"
87

98
"github.com/v2ray/v2ray-core"
109
socksio "github.com/v2ray/v2ray-core/io/socks"
1110
"github.com/v2ray/v2ray-core/log"
11+
v2net "github.com/v2ray/v2ray-core/net"
1212
)
1313

1414
var (
@@ -127,31 +127,14 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
127127
}
128128

129129
func (server *SocksServer) dumpInput(conn net.Conn, input chan<- []byte, finish chan<- bool) {
130-
for {
131-
buffer := make([]byte, 512)
132-
nBytes, err := conn.Read(buffer)
133-
log.Debug("Reading %d bytes, with error %v", nBytes, err)
134-
if err == io.EOF {
135-
close(input)
136-
finish <- true
137-
log.Debug("Socks finishing input.")
138-
break
139-
}
140-
input <- buffer[:nBytes]
141-
}
130+
v2net.ReaderToChan(input, conn)
131+
close(input)
132+
finish <- true
142133
}
143134

144135
func (server *SocksServer) dumpOutput(conn net.Conn, output <-chan []byte, finish chan<- bool) {
145-
for {
146-
buffer, open := <-output
147-
if !open {
148-
finish <- true
149-
log.Debug("Socks finishing output")
150-
break
151-
}
152-
nBytes, err := conn.Write(buffer)
153-
log.Debug("Writing %d bytes with error %v", nBytes, err)
154-
}
136+
v2net.ChanToWriter(conn, output)
137+
finish <- true
155138
}
156139

157140
func (server *SocksServer) waitForFinish(finish <-chan bool) {

‎net/transport.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net
2+
3+
import (
4+
"io"
5+
6+
"github.com/v2ray/v2ray-core/log"
7+
)
8+
9+
const (
10+
bufferSize = 8192
11+
)
12+
13+
func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
14+
for {
15+
buffer := make([]byte, bufferSize)
16+
nBytes, err := reader.Read(buffer)
17+
if err != nil {
18+
return err
19+
}
20+
stream <- buffer[:nBytes]
21+
}
22+
return nil
23+
}
24+
25+
func ChanToWriter(writer io.Writer, stream <-chan []byte) error {
26+
for buffer := range stream {
27+
nBytes, err := writer.Write(buffer)
28+
log.Debug("Writing %d bytes with error %v", nBytes, err)
29+
if err != nil {
30+
return err
31+
}
32+
}
33+
return nil
34+
}

‎net/vmess/vmess.go

-5
This file was deleted.

‎net/vmess/vmessin.go

+6-22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
v2io "github.com/v2ray/v2ray-core/io"
1111
vmessio "github.com/v2ray/v2ray-core/io/vmess"
1212
"github.com/v2ray/v2ray-core/log"
13+
v2net "github.com/v2ray/v2ray-core/net"
1314
)
1415

1516
type VMessInboundHandler struct {
@@ -92,31 +93,14 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
9293
}
9394

9495
func (handler *VMessInboundHandler) dumpInput(reader io.Reader, input chan<- []byte, finish chan<- bool) {
95-
for {
96-
buffer := make([]byte, BufferSize)
97-
nBytes, err := reader.Read(buffer)
98-
log.Debug("VMessInbound: Reading %d bytes with error %v", nBytes, err)
99-
if err == io.EOF {
100-
close(input)
101-
log.Debug("VMessInbound finishing input.")
102-
finish <- true
103-
break
104-
}
105-
input <- buffer[:nBytes]
106-
}
96+
v2net.ReaderToChan(input, reader)
97+
close(input)
98+
finish <- true
10799
}
108100

109101
func (handler *VMessInboundHandler) dumpOutput(writer io.Writer, output <-chan []byte, finish chan<- bool) {
110-
for {
111-
buffer, open := <-output
112-
if !open {
113-
finish <- true
114-
log.Debug("VMessInbound finishing output.")
115-
break
116-
}
117-
nBytes, err := writer.Write(buffer)
118-
log.Debug("VmessInbound: Wrote %d bytes with error %v", nBytes, err)
119-
}
102+
v2net.ChanToWriter(writer, output)
103+
finish <- true
120104
}
121105

122106
func (handler *VMessInboundHandler) waitForFinish(finish <-chan bool) {

‎net/vmess/vmessout.go

+5-22
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,14 @@ func (handler *VMessOutboundHandler) startCommunicate(request *vmessio.VMessRequ
118118
}
119119

120120
func (handler *VMessOutboundHandler) dumpOutput(reader io.Reader, output chan<- []byte, finish chan<- bool) {
121-
for {
122-
buffer := make([]byte, BufferSize)
123-
nBytes, err := reader.Read(buffer)
124-
log.Debug("VMessOutbound: Reading %d bytes, with error %v", nBytes, err)
125-
if err == io.EOF {
126-
close(output)
127-
finish <- true
128-
log.Debug("VMessOutbound finishing output.")
129-
break
130-
}
131-
output <- buffer[:nBytes]
132-
}
121+
v2net.ReaderToChan(output, reader)
122+
close(output)
123+
finish <- true
133124
}
134125

135126
func (handler *VMessOutboundHandler) dumpInput(writer io.Writer, input <-chan []byte, finish chan<- bool) {
136-
for {
137-
buffer, open := <-input
138-
if !open {
139-
finish <- true
140-
log.Debug("VMessOutbound finishing input.")
141-
break
142-
}
143-
nBytes, err := writer.Write(buffer)
144-
log.Debug("VMessOutbound: Wrote %d bytes with error %v", nBytes, err)
145-
}
127+
v2net.ChanToWriter(writer, input)
128+
finish <- true
146129
}
147130

148131
func (handler *VMessOutboundHandler) waitForFinish(finish <-chan bool) {

‎release/server/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/v2ray/v2ray-core"
99
"github.com/v2ray/v2ray-core/log"
1010

11-
// The following are neccesary as they register handlers in their init functions.
11+
// The following are neccesary as they register handlers in their init functions.
1212
_ "github.com/v2ray/v2ray-core/net/freedom"
1313
_ "github.com/v2ray/v2ray-core/net/socks"
1414
_ "github.com/v2ray/v2ray-core/net/vmess"

0 commit comments

Comments
 (0)