mirror of https://github.com/v2ray/v2ray-core
format code
parent
262eb4f985
commit
9e6b746e86
|
@ -5,7 +5,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
_ "log"
|
_ "log"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/net"
|
||||||
)
|
)
|
||||||
|
@ -26,7 +26,7 @@ type Socks5AuthenticationRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
|
func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
|
||||||
for i := 0; i < int(request.nMethods); i++ {
|
for i := 0; i < int(request.nMethods); i++ {
|
||||||
if request.authMethods[i] == method {
|
if request.authMethods[i] == method {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -142,16 +142,16 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
}
|
}
|
||||||
case AddrTypeDomain:
|
case AddrTypeDomain:
|
||||||
buffer = make([]byte, 256)
|
buffer = make([]byte, 256)
|
||||||
nBytes, err = reader.Read(buffer[0:1])
|
nBytes, err = reader.Read(buffer[0:1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
domainLength := buffer[0]
|
domainLength := buffer[0]
|
||||||
nBytes, err = reader.Read(buffer[:domainLength])
|
nBytes, err = reader.Read(buffer[:domainLength])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if nBytes != int(domainLength) {
|
if nBytes != int(domainLength) {
|
||||||
err = fmt.Errorf("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
|
err = fmt.Errorf("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,8 +2,8 @@ package freedom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/net"
|
||||||
|
@ -26,10 +26,10 @@ func (vconn *VFreeConnection) Start(vRay core.OutboundVRay) error {
|
||||||
output := vRay.OutboundOutput()
|
output := vRay.OutboundOutput()
|
||||||
conn, err := net.Dial("tcp", vconn.dest.String())
|
conn, err := net.Dial("tcp", vconn.dest.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Print("Working on tcp:" + vconn.dest.String())
|
log.Print("Working on tcp:" + vconn.dest.String())
|
||||||
|
|
||||||
finish := make(chan bool, 2)
|
finish := make(chan bool, 2)
|
||||||
go vconn.DumpInput(conn, input, finish)
|
go vconn.DumpInput(conn, input, finish)
|
||||||
|
@ -54,11 +54,11 @@ func (vconn *VFreeConnection) DumpOutput(conn net.Conn, output chan<- []byte, fi
|
||||||
buffer := make([]byte, 128)
|
buffer := make([]byte, 128)
|
||||||
nBytes, err := conn.Read(buffer)
|
nBytes, err := conn.Read(buffer)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
close(output)
|
close(output)
|
||||||
finish <- true
|
finish <- true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
log.Print(buffer[:nBytes])
|
log.Print(buffer[:nBytes])
|
||||||
output <- buffer[:nBytes]
|
output <- buffer[:nBytes]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package freedom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreedomFactory struct {
|
type FreedomFactory struct {
|
||||||
|
|
|
@ -3,9 +3,9 @@ package socks
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
socksio "github.com/v2ray/v2ray-core/io/socks"
|
socksio "github.com/v2ray/v2ray-core/io/socks"
|
||||||
|
@ -42,7 +42,7 @@ func (server *SocksServer) AcceptConnections(listener net.Listener) error {
|
||||||
for server.accepting {
|
for server.accepting {
|
||||||
connection, err := listener.Accept()
|
connection, err := listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
go server.HandleConnection(connection)
|
go server.HandleConnection(connection)
|
||||||
|
@ -55,14 +55,14 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
||||||
|
|
||||||
auth, err := socksio.ReadAuthentication(connection)
|
auth, err := socksio.ReadAuthentication(connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Print(auth)
|
log.Print(auth)
|
||||||
|
|
||||||
if !auth.HasAuthMethod(socksio.AuthNotRequired) {
|
if !auth.HasAuthMethod(socksio.AuthNotRequired) {
|
||||||
// TODO send response with FF
|
// TODO send response with FF
|
||||||
log.Print(ErrorAuthenticationFailed)
|
log.Print(ErrorAuthenticationFailed)
|
||||||
return ErrorAuthenticationFailed
|
return ErrorAuthenticationFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,33 +71,32 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
||||||
|
|
||||||
request, err := socksio.ReadRequest(connection)
|
request, err := socksio.ReadRequest(connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := socksio.NewSocks5Response()
|
response := socksio.NewSocks5Response()
|
||||||
|
|
||||||
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
|
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
|
||||||
response := socksio.NewSocks5Response()
|
response := socksio.NewSocks5Response()
|
||||||
response.Error = socksio.ErrorCommandNotSupported
|
response.Error = socksio.ErrorCommandNotSupported
|
||||||
socksio.WriteResponse(connection, response)
|
socksio.WriteResponse(connection, response)
|
||||||
log.Print(ErrorCommandNotSupported)
|
log.Print(ErrorCommandNotSupported)
|
||||||
return ErrorCommandNotSupported
|
return ErrorCommandNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
response.Error = socksio.ErrorSuccess
|
response.Error = socksio.ErrorSuccess
|
||||||
response.Port = request.Port
|
response.Port = request.Port
|
||||||
response.AddrType = request.AddrType
|
response.AddrType = request.AddrType
|
||||||
switch response.AddrType {
|
switch response.AddrType {
|
||||||
case socksio.AddrTypeIPv4:
|
case socksio.AddrTypeIPv4:
|
||||||
copy(response.IPv4[:], request.IPv4[:])
|
copy(response.IPv4[:], request.IPv4[:])
|
||||||
case socksio.AddrTypeIPv6:
|
case socksio.AddrTypeIPv6:
|
||||||
copy(response.IPv6[:], request.IPv6[:])
|
copy(response.IPv6[:], request.IPv6[:])
|
||||||
case socksio.AddrTypeDomain:
|
case socksio.AddrTypeDomain:
|
||||||
response.Domain = request.Domain
|
response.Domain = request.Domain
|
||||||
}
|
}
|
||||||
socksio.WriteResponse(connection, response)
|
socksio.WriteResponse(connection, response)
|
||||||
|
|
||||||
|
|
||||||
ray := server.vPoint.NewInboundConnectionAccepted(request.Destination())
|
ray := server.vPoint.NewInboundConnectionAccepted(request.Destination())
|
||||||
input := ray.InboundInput()
|
input := ray.InboundInput()
|
||||||
|
@ -115,9 +114,9 @@ func (server *SocksServer) dumpInput(conn net.Conn, input chan<- []byte, finish
|
||||||
for {
|
for {
|
||||||
buffer := make([]byte, 256)
|
buffer := make([]byte, 256)
|
||||||
nBytes, err := conn.Read(buffer)
|
nBytes, err := conn.Read(buffer)
|
||||||
log.Printf("Reading %d bytes, with error %v", nBytes, err)
|
log.Printf("Reading %d bytes, with error %v", nBytes, err)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
close(input)
|
close(input)
|
||||||
finish <- true
|
finish <- true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -133,7 +132,7 @@ func (server *SocksServer) dumpOutput(conn net.Conn, output <-chan []byte, finis
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
nBytes, _ := conn.Write(buffer)
|
nBytes, _ := conn.Write(buffer)
|
||||||
log.Printf("Writing %d bytes", nBytes)
|
log.Printf("Writing %d bytes", nBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
net/vdest.go
10
net/vdest.go
|
@ -50,11 +50,11 @@ func (addr VAddress) String() string {
|
||||||
var host string
|
var host string
|
||||||
switch addr.Type {
|
switch addr.Type {
|
||||||
case AddrTypeIP:
|
case AddrTypeIP:
|
||||||
host = addr.IP.String()
|
host = addr.IP.String()
|
||||||
if len(addr.IP) == net.IPv6len {
|
if len(addr.IP) == net.IPv6len {
|
||||||
host = "[" + host + "]"
|
host = "[" + host + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
case AddrTypeDomain:
|
case AddrTypeDomain:
|
||||||
host = addr.Domain
|
host = addr.Domain
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
"github.com/v2ray/v2ray-core/net/freedom"
|
"github.com/v2ray/v2ray-core/net/freedom"
|
||||||
"github.com/v2ray/v2ray-core/net/socks"
|
"github.com/v2ray/v2ray-core/net/socks"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
port := uint16(8888)
|
port := uint16(8888)
|
||||||
|
|
||||||
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
|
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
|
||||||
vid, err := core.UUIDToVID(uuid)
|
vid, err := core.UUIDToVID(uuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
config := core.VConfig{
|
config := core.VConfig{
|
||||||
port,
|
port,
|
||||||
|
@ -24,14 +24,14 @@ func main() {
|
||||||
[]core.VNext{}}
|
[]core.VNext{}}
|
||||||
|
|
||||||
vpoint, err := core.NewVPoint(&config, socks.SocksServerFactory{}, freedom.FreedomFactory{})
|
vpoint, err := core.NewVPoint(&config, socks.SocksServerFactory{}, freedom.FreedomFactory{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
err = vpoint.Start()
|
err = vpoint.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
finish := make(chan bool)
|
finish := make(chan bool)
|
||||||
<-finish
|
<-finish
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,9 @@ func NewVPoint(config *VConfig, ichFactory InboundConnectionHandlerFactory, ochF
|
||||||
for _, user := range vpoint.Config.AllowedClients {
|
for _, user := range vpoint.Config.AllowedClients {
|
||||||
vpoint.UserSet.AddUser(user)
|
vpoint.UserSet.AddUser(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
vpoint.ichFactory = ichFactory
|
vpoint.ichFactory = ichFactory
|
||||||
vpoint.ochFactory = ochFactory
|
vpoint.ochFactory = ochFactory
|
||||||
|
|
||||||
return vpoint, nil
|
return vpoint, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue