1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00
This commit is contained in:
Darien Raymond 2017-02-22 20:56:10 +01:00
commit f4012d38dd
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
8 changed files with 94 additions and 116 deletions

View File

@ -24,33 +24,49 @@ type Error struct {
} }
// Error implements error.Error(). // Error implements error.Error().
func (v *Error) Error() string { func (v Error) Error() string {
return v.message return v.message
} }
// Inner implements hasInnerError.Inner() // Inner implements hasInnerError.Inner()
func (v *Error) Inner() error { func (v Error) Inner() error {
if v.inner == nil { if v.inner == nil {
return nil return nil
} }
return v.inner return v.inner
} }
func (v *Error) ActionRequired() bool { func (v Error) ActionRequired() bool {
return v.actionRequired return v.actionRequired
} }
// New returns a new error object with message formed from given arguments. func (v Error) RequireUserAction() Error {
func New(msg ...interface{}) error { v.actionRequired = true
return &Error{ return v
}
func (v Error) Message(msg ...interface{}) Error {
return Error{
inner: v,
message: serial.Concat(msg...), message: serial.Concat(msg...),
} }
} }
// Base returns an ErrorBuilder based on the given error. func (v Error) Format(format string, values ...interface{}) Error {
func Base(err error) ErrorBuilder { return v.Message(fmt.Sprintf(format, values...))
return ErrorBuilder{ }
error: err,
// New returns a new error object with message formed from given arguments.
func New(msg ...interface{}) Error {
return Error{
message: serial.Concat(msg...),
}
}
// Base returns an Error based on the given error.
func Base(err error) Error {
return Error{
inner: err,
} }
} }
@ -86,38 +102,3 @@ func IsActionRequired(err error) bool {
} }
return false return false
} }
type ErrorBuilder struct {
error
actionRequired bool
}
func (v ErrorBuilder) RequireUserAction() ErrorBuilder {
v.actionRequired = true
return v
}
// Message returns an error object with given message and base error.
func (v ErrorBuilder) Message(msg ...interface{}) error {
if v.error == nil {
return nil
}
return &Error{
message: serial.Concat(msg...) + " > " + v.error.Error(),
inner: v.error,
actionRequired: v.actionRequired,
}
}
// Format returns an errors object with given message format and base error.
func (v ErrorBuilder) Format(format string, values ...interface{}) error {
if v.error == nil {
return nil
}
return &Error{
message: fmt.Sprintf(format, values...) + " > " + v.error.Error(),
inner: v.error,
actionRequired: v.actionRequired,
}
}

View File

@ -5,6 +5,7 @@ import (
"crypto/md5" "crypto/md5"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
) )
@ -15,42 +16,8 @@ var (
type UUID [16]byte type UUID [16]byte
// String returns the string representation of this UUID. // String returns the string representation of this UUID.
func (v *UUID) String() string { func (u *UUID) String() string {
return bytesToString(v.Bytes()) bytes := u.Bytes()
}
// Bytes returns the bytes representation of this UUID.
func (v *UUID) Bytes() []byte {
return v[:]
}
// Equals returns true if this UUID equals another UUID by value.
func (v *UUID) Equals(another *UUID) bool {
if v == nil && another == nil {
return true
}
if v == nil || another == nil {
return false
}
return bytes.Equal(v.Bytes(), another.Bytes())
}
// Next generates a deterministic random UUID based on this UUID.
func (v *UUID) Next() *UUID {
md5hash := md5.New()
md5hash.Write(v.Bytes())
md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
newid := new(UUID)
for {
md5hash.Sum(newid[:0])
if !newid.Equals(v) {
return newid
}
md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
}
}
func bytesToString(bytes []byte) string {
result := hex.EncodeToString(bytes[0 : byteGroups[0]/2]) result := hex.EncodeToString(bytes[0 : byteGroups[0]/2])
start := byteGroups[0] / 2 start := byteGroups[0] / 2
for i := 1; i < len(byteGroups); i++ { for i := 1; i < len(byteGroups); i++ {
@ -62,6 +29,37 @@ func bytesToString(bytes []byte) string {
return result return result
} }
// Bytes returns the bytes representation of this UUID.
func (u *UUID) Bytes() []byte {
return u[:]
}
// Equals returns true if this UUID equals another UUID by value.
func (u *UUID) Equals(another *UUID) bool {
if u == nil && another == nil {
return true
}
if u == nil || another == nil {
return false
}
return bytes.Equal(u.Bytes(), another.Bytes())
}
// Next generates a deterministic random UUID based on this UUID.
func (u *UUID) Next() *UUID {
md5hash := md5.New()
md5hash.Write(u.Bytes())
md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
newid := new(UUID)
for {
md5hash.Sum(newid[:0])
if !newid.Equals(u) {
return newid
}
md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
}
}
// New creates an UUID with random value. // New creates an UUID with random value.
func New() *UUID { func New() *UUID {
uuid := new(UUID) uuid := new(UUID)
@ -69,7 +67,7 @@ func New() *UUID {
return uuid return uuid
} }
// PraseBytes converts an UUID in byte form to object. // ParseBytes converts an UUID in byte form to object.
func ParseBytes(b []byte) (*UUID, error) { func ParseBytes(b []byte) (*UUID, error) {
if len(b) != 16 { if len(b) != 16 {
return nil, errors.New("Invalid UUID: ", b) return nil, errors.New("Invalid UUID: ", b)

View File

@ -22,7 +22,8 @@ var (
intro = "An unified platform for anti-censorship." intro = "An unified platform for anti-censorship."
) )
// Version returns V2Ray's version as a string. // Version returns V2Ray's version as a string, in the form of "x.y.z" where x, y and z are numbers.
// ".z" part may be omitted in regular releases.
func Version() string { func Version() string {
return version return version
} }

View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"v2ray.com/core/common"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
) )
@ -38,5 +39,5 @@ func loadProtobufConfig(input io.Reader) (*Config, error) {
} }
func init() { func init() {
RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig) common.Must(RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig))
} }

View File

@ -43,7 +43,7 @@ func GetConfigFormat() core.ConfigFormat {
} }
} }
func startV2Ray() (*core.Point, error) { func startV2Ray() (core.Server, error) {
if len(configFile) == 0 { if len(configFile) == 0 {
return nil, errors.New("V2Ray: Config file is not set.") return nil, errors.New("V2Ray: Config file is not set.")
} }
@ -64,7 +64,7 @@ func startV2Ray() (*core.Point, error) {
return nil, errors.Base(err).Message("V2Ray: Failed to read config file: ", configFile) return nil, errors.Base(err).Message("V2Ray: Failed to read config file: ", configFile)
} }
vPoint, err := core.NewPoint(config) vPoint, err := core.New(config)
if err != nil { if err != nil {
return nil, errors.Base(err).Message("V2Ray: Failed to create initialize.") return nil, errors.Base(err).Message("V2Ray: Failed to create initialize.")
} }
@ -81,7 +81,7 @@ func main() {
return return
} }
point, err := startV2Ray() server, err := startV2Ray()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return return
@ -92,7 +92,7 @@ func main() {
return return
} }
if err := point.Start(); err != nil { if err := server.Start(); err != nil {
fmt.Println("V2Ray: Failed to start. ", err) fmt.Println("V2Ray: Failed to start. ", err)
} }
@ -100,5 +100,5 @@ func main() {
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM) signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
<-osSignals <-osSignals
point.Close() server.Close()
} }

View File

@ -159,7 +159,7 @@ func (s *ServerSession) Handshake(reader io.Reader, writer io.Writer) (*protocol
if err := buffer.AppendSupplier(buf.ReadFullFrom(reader, domainLength)); err != nil { if err := buffer.AppendSupplier(buf.ReadFullFrom(reader, domainLength)); err != nil {
return nil, err return nil, err
} }
request.Address = v2net.DomainAddress(string(buffer.BytesFrom(-domainLength))) request.Address = v2net.ParseAddress(string(buffer.BytesFrom(-domainLength)))
default: default:
return nil, errors.New("Socks|Server: Unknown address type: ", addrType) return nil, errors.New("Socks|Server: Unknown address type: ", addrType)
} }
@ -400,10 +400,10 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
} }
if b.Byte(0) != socks5Version { if b.Byte(0) != socks5Version {
return nil, errors.New("Socks|Client: Unexpected server version: ", b.Byte(0)) return nil, errors.New("Socks|Client: Unexpected server version: ", b.Byte(0)).RequireUserAction()
} }
if b.Byte(1) != authByte { if b.Byte(1) != authByte {
return nil, errors.New("Socks|Client: auth method not supported.") return nil, errors.New("Socks|Client: auth method not supported.").RequireUserAction()
} }
if authByte == authPassword { if authByte == authPassword {

View File

@ -29,7 +29,7 @@ type Server struct {
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
space := app.SpaceFromContext(ctx) space := app.SpaceFromContext(ctx)
if space == nil { if space == nil {
return nil, errors.New("Socks|Server: No space in context.") return nil, errors.New("Socks|Server: No space in context.").RequireUserAction()
} }
s := &Server{ s := &Server{
config: config, config: config,
@ -130,8 +130,7 @@ func (v *Server) transport(ctx context.Context, reader io.Reader, writer io.Writ
v2reader := buf.NewReader(reader) v2reader := buf.NewReader(reader)
if err := buf.PipeUntilEOF(timer, v2reader, input); err != nil { if err := buf.PipeUntilEOF(timer, v2reader, input); err != nil {
log.Info("Socks|Server: Failed to transport all TCP request: ", err) return errors.Base(err).Message("Socks|Server: Failed to transport all TCP request.")
return err
} }
return nil return nil
}) })
@ -139,17 +138,15 @@ func (v *Server) transport(ctx context.Context, reader io.Reader, writer io.Writ
responseDone := signal.ExecuteAsync(func() error { responseDone := signal.ExecuteAsync(func() error {
v2writer := buf.NewWriter(writer) v2writer := buf.NewWriter(writer)
if err := buf.PipeUntilEOF(timer, output, v2writer); err != nil { if err := buf.PipeUntilEOF(timer, output, v2writer); err != nil {
log.Info("Socks|Server: Failed to transport all TCP response: ", err) return errors.Base(err).Message("Socks|Server: Failed to transport all TCP response.")
return err
} }
return nil return nil
}) })
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil { if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
log.Info("Socks|Server: Connection ends with ", err)
input.CloseError() input.CloseError()
output.CloseError() output.CloseError()
return err return errors.Base(err).Message("Socks|Server: Connection ends.")
} }
runtime.KeepAlive(timer) runtime.KeepAlive(timer)

View File

@ -23,20 +23,20 @@ type Server interface {
Close() Close()
} }
// Point shell of V2Ray. // New creates a new V2Ray server with given config.
type Point struct { func New(config *Config) (Server, error) {
return newSimpleServer(config)
}
// simpleServer shell of V2Ray.
type simpleServer struct {
space app.Space space app.Space
} }
// New creates a new V2Ray server with given config. // newSimpleServer returns a new Point server based on given configuration.
func New(config *Config) (Server, error) {
return NewPoint(config)
}
// NewPoint returns a new Point server based on given configuration.
// The server is not started at this point. // The server is not started at this point.
func NewPoint(config *Config) (*Point, error) { func newSimpleServer(config *Config) (*simpleServer, error) {
var pt = new(Point) var server = new(simpleServer)
if err := config.Transport.Apply(); err != nil { if err := config.Transport.Apply(); err != nil {
return nil, err return nil, err
@ -45,7 +45,7 @@ func NewPoint(config *Config) (*Point, error) {
space := app.NewSpace() space := app.NewSpace()
ctx := app.ContextWithSpace(context.Background(), space) ctx := app.ContextWithSpace(context.Background(), space)
pt.space = space server.space = space
for _, appSettings := range config.App { for _, appSettings := range config.App {
settings, err := appSettings.GetInstance() settings, err := appSettings.GetInstance()
@ -132,19 +132,19 @@ func NewPoint(config *Config) (*Point, error) {
} }
} }
if err := pt.space.Initialize(); err != nil { if err := server.space.Initialize(); err != nil {
return nil, err return nil, err
} }
return pt, nil return server, nil
} }
func (v *Point) Close() { func (s *simpleServer) Close() {
v.space.Close() s.space.Close()
} }
func (v *Point) Start() error { func (s *simpleServer) Start() error {
if err := v.space.Start(); err != nil { if err := s.space.Start(); err != nil {
return err return err
} }
log.Warning("V2Ray started.") log.Warning("V2Ray started.")