V2Ray 10 年 前
コミット
1c618e93b1
4 ファイル変更67 行追加17 行削除
  1. 7 4
      net/freedom/freedom.go
  2. 21 4
      vpoint.go
  3. 39 0
      vray.go
  4. 0 9
      vsegment.go

+ 7 - 4
net/freedom/freedom.go

@@ -3,6 +3,8 @@ package tcp
 import (
 	"io"
 	"net"
+
+	"github.com/v2ray/v2ray-core"
 )
 
 type VFreeConnection struct {
@@ -17,18 +19,19 @@ func NewVFreeConnection(network string, address string) *VFreeConnection {
 	return conn
 }
 
-func (vconn *VFreeConnection) Start(input <-chan []byte) chan<- []byte {
-	output := make(chan []byte, 128)
+func (vconn *VFreeConnection) Start(vRay core.OutboundVRay) error {
+	input := vRay.OutboundInput()
+	output := vRay.OutboundOutput()
 	conn, err := net.Dial(vconn.network, vconn.address)
 	if err != nil {
-		panic(err)
+		return err
 	}
 
 	finish := make(chan bool, 2)
 	go vconn.DumpInput(conn, input, finish)
 	go vconn.DumpOutput(conn, output, finish)
 	go vconn.CloseConn(conn, finish)
-	return output
+	return nil
 }
 
 func (vconn *VFreeConnection) DumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {

+ 21 - 4
vpoint.go

@@ -6,8 +6,9 @@ import (
 
 // VPoint is an single server in V2Ray system.
 type VPoint struct {
-	config      VConfig
-	connHandler ConnectionHandler
+	config     VConfig
+	ichFactory InboundConnectionHandlerFactory
+	ochFactory OutboundConnectionHandlerFactory
 }
 
 // NewVPoint returns a new VPoint server based on given configuration.
@@ -18,16 +19,32 @@ func NewVPoint(config *VConfig) (*VPoint, error) {
 	return vpoint, nil
 }
 
-type ConnectionHandler interface {
+type InboundConnectionHandlerFactory interface {
+	Create(vPoint *VPoint) (InboundConnectionHandler, error)
+}
+
+type InboundConnectionHandler interface {
 	Listen(port uint16) error
 }
 
+type OutboundConnectionHandlerFactory interface {
+	Create(vPoint *VPoint) (OutboundConnectionHandler, error)
+}
+
+type OutboundConnectionHandler interface {
+	Start(vray *OutboundVRay) error
+}
+
 // Start starts the VPoint server, and return any error during the process.
 // In the case of any errors, the state of the server is unpredicatable.
 func (vp *VPoint) Start() error {
 	if vp.config.Port <= 0 {
 		return fmt.Errorf("Invalid port %d", vp.config.Port)
 	}
-	vp.connHandler.Listen(vp.config.Port)
+	inboundConnectionHandler, err := vp.ichFactory.Create(vp)
+	if err != nil {
+		return err
+	}
+	err = inboundConnectionHandler.Listen(vp.config.Port)
 	return nil
 }

+ 39 - 0
vray.go

@@ -0,0 +1,39 @@
+package core
+
+type VRay struct {
+	Input  chan []byte
+	Output chan []byte
+}
+
+func NewVRay() *VRay {
+	ray := new(VRay)
+	ray.Input = make(chan []byte, 128)
+	ray.Output = make(chan []byte, 128)
+	return ray
+}
+
+type OutboundVRay interface {
+	OutboundInput() <-chan []byte
+	OutboundOutput() chan<- []byte
+}
+
+type InboundVRay interface {
+	InboundInput() chan<- []byte
+	OutboundOutput() <-chan []byte
+}
+
+func (ray *VRay) OutboundInput() <-chan []byte {
+	return ray.Input
+}
+
+func (ray *VRay) OutboundOutput() chan<- []byte {
+	return ray.Output
+}
+
+func (ray *VRay) InboundInput() chan<- []byte {
+	return ray.Input
+}
+
+func (ray *VRay) InboundOutput() <-chan []byte {
+	return ray.Output
+}

+ 0 - 9
vsegment.go

@@ -1,9 +0,0 @@
-package core
-
-// VSegment is a connection between 2 VPoints
-type VSegment struct {
-}
-
-func NewVSegment() *VSegment {
-	return new(VSegment)
-}