This commit is contained in:
unknown
2019-10-15 17:12:27 +08:00
parent f4ae503982
commit 6771816be7
8 changed files with 153 additions and 86 deletions

View File

@@ -12,25 +12,25 @@ type Handshake struct {
core.NpsPlugin
}
func (handshake *Handshake) Run(ctx context.Context, config map[string]string) error {
func (handshake *Handshake) Run(ctx context.Context, config map[string]string) (context.Context, error) {
clientConn := handshake.GetClientConn(ctx)
buf := make([]byte, 2)
if _, err := io.ReadFull(clientConn, buf); err != nil {
return errors.New("negotiation err while read 2 bytes from client connection: " + err.Error())
return ctx, errors.New("negotiation err while read 2 bytes from client connection: " + err.Error())
}
if version := buf[0]; version != 5 {
return errors.New("only support socks5")
return ctx, errors.New("only support socks5")
}
nMethods := buf[1]
methods := make([]byte, nMethods)
if n, err := clientConn.Read(methods); n != int(nMethods) || err != nil {
return errors.New(fmt.Sprintf("read methods error, need %d , read %d, error %s", nMethods, n, err.Error()))
return ctx, errors.New(fmt.Sprintf("read methods error, need %d , read %d, error %s", nMethods, n, err.Error()))
} else {
context.WithValue(ctx, "methods", methods[:n])
ctx = context.WithValue(ctx, "methods", methods[:n])
}
return nil
return ctx, nil
}