This commit is contained in:
Andy Wang 2020-08-17 17:56:05 +01:00
parent 9023f3f897
commit 0b217ddb07
7 changed files with 33 additions and 32 deletions

View File

@ -1,5 +1,6 @@
[![Build Status](https://travis-ci.org/cbeuw/Cloak.svg?branch=master)](https://travis-ci.org/cbeuw/Cloak) [![Build Status](https://travis-ci.org/cbeuw/Cloak.svg?branch=master)](https://travis-ci.org/cbeuw/Cloak)
[![codecov](https://codecov.io/gh/cbeuw/Cloak/branch/master/graph/badge.svg)](https://codecov.io/gh/cbeuw/Cloak) [![codecov](https://codecov.io/gh/cbeuw/Cloak/branch/master/graph/badge.svg)](https://codecov.io/gh/cbeuw/Cloak)
[![Go Report Card](https://goreportcard.com/badge/github.com/cbeuw/Cloak)](https://goreportcard.com/report/github.com/cbeuw/Cloak)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SAUYKGSREP8GL&source=url) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SAUYKGSREP8GL&source=url)

View File

@ -49,7 +49,7 @@ func main() {
flag.StringVar(&remoteHost, "s", "", "remoteHost: IP of your proxy server") flag.StringVar(&remoteHost, "s", "", "remoteHost: IP of your proxy server")
flag.StringVar(&remotePort, "p", "443", "remotePort: proxy port, should be 443") flag.StringVar(&remotePort, "p", "443", "remotePort: proxy port, should be 443")
flag.BoolVar(&udp, "u", false, "udp: set this flag if the underlying proxy is using UDP protocol") flag.BoolVar(&udp, "u", false, "udp: set this flag if the underlying proxy is using UDP protocol")
flag.StringVar(&config, "c", "ckclient.json", "config: path to the configuration file or options seperated with semicolons") flag.StringVar(&config, "c", "ckclient.json", "config: path to the configuration file or options separated with semicolons")
flag.StringVar(&proxyMethod, "proxy", "", "proxy: the proxy method's name. It must match exactly with the corresponding entry in server's ProxyBook") flag.StringVar(&proxyMethod, "proxy", "", "proxy: the proxy method's name. It must match exactly with the corresponding entry in server's ProxyBook")
flag.StringVar(&b64AdminUID, "a", "", "adminUID: enter the adminUID to serve the admin api") flag.StringVar(&b64AdminUID, "a", "", "adminUID: enter the adminUID to serve the admin api")
askVersion := flag.Bool("v", false, "Print the version number") askVersion := flag.Bool("v", false, "Print the version number")

View File

@ -92,8 +92,7 @@ func main() {
bindAddr, err := parseBindAddr(raw.BindAddr) bindAddr, err := parseBindAddr(raw.BindAddr)
if err != nil { if err != nil {
err = fmt.Errorf("unable to parse BindAddr: %v", err) log.Fatalf("unable to parse BindAddr: %v", err)
return
} }
if !pluginMode && len(bindAddr) == 0 { if !pluginMode && len(bindAddr) == 0 {
https, _ := net.ResolveTCPAddr("tcp", ":443") https, _ := net.ResolveTCPAddr("tcp", ":443")

View File

@ -8,7 +8,7 @@ import (
// Valve needs to be universal, across all sessions that belong to a user // Valve needs to be universal, across all sessions that belong to a user
type LimitedValve struct { type LimitedValve struct {
// traffic directions from the server's perspective are refered // traffic directions from the server's perspective are referred
// exclusively as rx and tx. // exclusively as rx and tx.
// rx is from client to server, tx is from server to client // rx is from client to server, tx is from server to client
// DO NOT use terms up or down as this is used in usermanager // DO NOT use terms up or down as this is used in usermanager

View File

@ -31,7 +31,6 @@ const (
) )
var ErrTimestampOutOfWindow = errors.New("timestamp is outside of the accepting window") var ErrTimestampOutOfWindow = errors.New("timestamp is outside of the accepting window")
var ErrUnrecognisedProtocol = errors.New("unrecognised protocol")
// decryptClientInfo checks if a the authFragments are valid. It doesn't check if the UID is authorised // decryptClientInfo checks if a the authFragments are valid. It doesn't check if the UID is authorised
func decryptClientInfo(fragments authFragments, serverTime time.Time) (info ClientInfo, err error) { func decryptClientInfo(fragments authFragments, serverTime time.Time) (info ClientInfo, err error) {

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"github.com/cbeuw/Cloak/internal/common" "github.com/cbeuw/Cloak/internal/common"
"github.com/cbeuw/Cloak/internal/server/usermanager" "github.com/cbeuw/Cloak/internal/server/usermanager"
@ -53,6 +54,8 @@ func connReadLine(conn net.Conn, buf []byte) (int, error) {
return i, io.ErrShortBuffer return i, io.ErrShortBuffer
} }
var ErrUnrecognisedProtocol = errors.New("unrecognised protocol")
func readFirstPacket(conn net.Conn, buf []byte, timeout time.Duration) (int, Transport, bool, error) { func readFirstPacket(conn net.Conn, buf []byte, timeout time.Duration) (int, Transport, bool, error) {
conn.SetReadDeadline(time.Now().Add(timeout)) conn.SetReadDeadline(time.Now().Add(timeout))
defer conn.SetReadDeadline(time.Time{}) defer conn.SetReadDeadline(time.Time{})
@ -113,7 +116,6 @@ func readFirstPacket(conn net.Conn, buf []byte, timeout time.Duration) (int, Tra
} }
} }
default: default:
err = fmt.Errorf("unrecognised protocol signature")
return bufOffset, transport, true, ErrUnrecognisedProtocol return bufOffset, transport, true, ErrUnrecognisedProtocol
} }
return bufOffset, transport, true, nil return bufOffset, transport, true, nil

View File

@ -7,8 +7,8 @@ import (
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
) )
var Uint32 = binary.BigEndian.Uint32 var u32 = binary.BigEndian.Uint32
var Uint64 = binary.BigEndian.Uint64 var u64 = binary.BigEndian.Uint64
func i64ToB(value int64) []byte { func i64ToB(value int64) []byte {
oct := make([]byte, 8) oct := make([]byte, 8)
@ -48,11 +48,11 @@ func (manager *localManager) AuthenticateUser(UID []byte) (int64, int64, error)
if bucket == nil { if bucket == nil {
return ErrUserNotFound return ErrUserNotFound
} }
upRate = int64(Uint64(bucket.Get([]byte("UpRate")))) upRate = int64(u64(bucket.Get([]byte("UpRate"))))
downRate = int64(Uint64(bucket.Get([]byte("DownRate")))) downRate = int64(u64(bucket.Get([]byte("DownRate"))))
upCredit = int64(Uint64(bucket.Get([]byte("UpCredit")))) upCredit = int64(u64(bucket.Get([]byte("UpCredit"))))
downCredit = int64(Uint64(bucket.Get([]byte("DownCredit")))) downCredit = int64(u64(bucket.Get([]byte("DownCredit"))))
expiryTime = int64(Uint64(bucket.Get([]byte("ExpiryTime")))) expiryTime = int64(u64(bucket.Get([]byte("ExpiryTime"))))
return nil return nil
}) })
if err != nil { if err != nil {
@ -83,10 +83,10 @@ func (manager *localManager) AuthoriseNewSession(UID []byte, ainfo Authorisation
if bucket == nil { if bucket == nil {
return ErrUserNotFound return ErrUserNotFound
} }
sessionsCap = int(Uint32(bucket.Get([]byte("SessionsCap")))) sessionsCap = int(u32(bucket.Get([]byte("SessionsCap"))))
upCredit = int64(Uint64(bucket.Get([]byte("UpCredit")))) upCredit = int64(u64(bucket.Get([]byte("UpCredit"))))
downCredit = int64(Uint64(bucket.Get([]byte("DownCredit")))) downCredit = int64(u64(bucket.Get([]byte("DownCredit"))))
expiryTime = int64(Uint64(bucket.Get([]byte("ExpiryTime")))) expiryTime = int64(u64(bucket.Get([]byte("ExpiryTime"))))
return nil return nil
}) })
if err != nil { if err != nil {
@ -129,7 +129,7 @@ func (manager *localManager) UploadStatus(uploads []StatusUpdate) ([]StatusRespo
responses = append(responses, resp) responses = append(responses, resp)
} }
oldUp := int64(Uint64(bucket.Get([]byte("UpCredit")))) oldUp := int64(u64(bucket.Get([]byte("UpCredit"))))
newUp := oldUp - status.UpUsage newUp := oldUp - status.UpUsage
if newUp <= 0 { if newUp <= 0 {
resp = StatusResponse{ resp = StatusResponse{
@ -144,7 +144,7 @@ func (manager *localManager) UploadStatus(uploads []StatusUpdate) ([]StatusRespo
log.Error(err) log.Error(err)
} }
oldDown := int64(Uint64(bucket.Get([]byte("DownCredit")))) oldDown := int64(u64(bucket.Get([]byte("DownCredit"))))
newDown := oldDown - status.DownUsage newDown := oldDown - status.DownUsage
if newDown <= 0 { if newDown <= 0 {
resp = StatusResponse{ resp = StatusResponse{
@ -159,7 +159,7 @@ func (manager *localManager) UploadStatus(uploads []StatusUpdate) ([]StatusRespo
log.Error(err) log.Error(err)
} }
expiry := int64(Uint64(bucket.Get([]byte("ExpiryTime")))) expiry := int64(u64(bucket.Get([]byte("ExpiryTime"))))
if manager.world.Now().Unix() > expiry { if manager.world.Now().Unix() > expiry {
resp = StatusResponse{ resp = StatusResponse{
status.UID, status.UID,
@ -179,12 +179,12 @@ func (manager *localManager) ListAllUsers() (infos []UserInfo, err error) {
err = tx.ForEach(func(UID []byte, bucket *bolt.Bucket) error { err = tx.ForEach(func(UID []byte, bucket *bolt.Bucket) error {
var uinfo UserInfo var uinfo UserInfo
uinfo.UID = UID uinfo.UID = UID
uinfo.SessionsCap = int32(Uint32(bucket.Get([]byte("SessionsCap")))) uinfo.SessionsCap = int32(u32(bucket.Get([]byte("SessionsCap"))))
uinfo.UpRate = int64(Uint64(bucket.Get([]byte("UpRate")))) uinfo.UpRate = int64(u64(bucket.Get([]byte("UpRate"))))
uinfo.DownRate = int64(Uint64(bucket.Get([]byte("DownRate")))) uinfo.DownRate = int64(u64(bucket.Get([]byte("DownRate"))))
uinfo.UpCredit = int64(Uint64(bucket.Get([]byte("UpCredit")))) uinfo.UpCredit = int64(u64(bucket.Get([]byte("UpCredit"))))
uinfo.DownCredit = int64(Uint64(bucket.Get([]byte("DownCredit")))) uinfo.DownCredit = int64(u64(bucket.Get([]byte("DownCredit"))))
uinfo.ExpiryTime = int64(Uint64(bucket.Get([]byte("ExpiryTime")))) uinfo.ExpiryTime = int64(u64(bucket.Get([]byte("ExpiryTime"))))
infos = append(infos, uinfo) infos = append(infos, uinfo)
return nil return nil
}) })
@ -200,12 +200,12 @@ func (manager *localManager) GetUserInfo(UID []byte) (uinfo UserInfo, err error)
return ErrUserNotFound return ErrUserNotFound
} }
uinfo.UID = UID uinfo.UID = UID
uinfo.SessionsCap = int32(Uint32(bucket.Get([]byte("SessionsCap")))) uinfo.SessionsCap = int32(u32(bucket.Get([]byte("SessionsCap"))))
uinfo.UpRate = int64(Uint64(bucket.Get([]byte("UpRate")))) uinfo.UpRate = int64(u64(bucket.Get([]byte("UpRate"))))
uinfo.DownRate = int64(Uint64(bucket.Get([]byte("DownRate")))) uinfo.DownRate = int64(u64(bucket.Get([]byte("DownRate"))))
uinfo.UpCredit = int64(Uint64(bucket.Get([]byte("UpCredit")))) uinfo.UpCredit = int64(u64(bucket.Get([]byte("UpCredit"))))
uinfo.DownCredit = int64(Uint64(bucket.Get([]byte("DownCredit")))) uinfo.DownCredit = int64(u64(bucket.Get([]byte("DownCredit"))))
uinfo.ExpiryTime = int64(Uint64(bucket.Get([]byte("ExpiryTime")))) uinfo.ExpiryTime = int64(u64(bucket.Get([]byte("ExpiryTime"))))
return nil return nil
}) })
return return