summaryrefslogtreecommitdiff
path: root/client/client.go
diff options
context:
space:
mode:
authoratanarjuat <atanarjuat@example.com>2022-06-06 14:40:14 +0200
committeratanarjuat <atanarjuat@example.com>2022-06-06 14:41:20 +0200
commit091c162ffd0cad17033b1ef6b3698f168193eeb4 (patch)
tree826a53368c2be8a2ccdf2393b2f6771f5b57a683 /client/client.go
parent823cb0ae2393db4186905226145e9791b202f0eb (diff)
status
Diffstat (limited to 'client/client.go')
-rw-r--r--client/client.go60
1 files changed, 43 insertions, 17 deletions
diff --git a/client/client.go b/client/client.go
index 2de8076..bd002f1 100644
--- a/client/client.go
+++ b/client/client.go
@@ -1,12 +1,14 @@
-// Package client exposes an obfsvpn client that launches a local socks5 proxy,
-// with optional kcp wire transport.
+// Package client exposes a socks5 proxy that uses obfs4 to communicate with the server,
+// with an optional kcp wire transport.
package client
import (
+ "errors"
"fmt"
"log"
"net"
+ "sync"
"0xacab.org/leap/obfsvpn"
@@ -14,11 +16,18 @@ import (
"github.com/xtaci/kcp-go"
)
+var (
+ ErrAlreadyRunning = errors.New("already initialized")
+ ErrNotRunning = errors.New("server not running")
+)
+
type Client struct {
kcp bool
socksAddr string
obfs4Cert string
- server *socks5.Server
+ server *socks5.Server
+ started bool
+ mu sync.Mutex
}
func NewClient(kcp bool, socksAddr, obfs4Cert string) *Client {
@@ -29,10 +38,13 @@ func NewClient(kcp bool, socksAddr, obfs4Cert string) *Client {
}
}
-func (c *Client) Start() bool {
+func (c *Client) Start() (bool, error) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+
if c.server != nil {
- log.Printf("Cannot start proxy server if already initialzied")
- return false
+ log.Printf("Cannot start proxy server if already initialized")
+ return false, ErrAlreadyRunning
}
c.server = &socks5.Server{
@@ -43,7 +55,7 @@ func (c *Client) Start() bool {
dialer, err := obfsvpn.NewDialerFromCert(c.obfs4Cert)
if err != nil {
log.Printf("Error getting dialer: %v\n", err)
- return false
+ return false, err
}
if c.kcp {
@@ -59,23 +71,37 @@ func (c *Client) Start() bool {
if err := c.server.ListenAndServe(); err != nil {
log.Printf("error while listening: %v\n", err)
c.server = nil
- return false
+ return false, err
}
- return true
+ c.started = true
+ return true, nil
}
+func (c *Client) Stop() (bool, error) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
-func (c *Client) Stop() bool {
if c.server == nil {
- return false
+ return false, ErrNotRunning
}
- defer func() {
- c.server = nil
- }()
if err := c.server.Close(); err != nil {
log.Printf("error while stopping: %v\n", err)
- return false
+ return false, err
+ }
+
+ c.server = nil
+ return true, nil
+}
+
+func (c *Client) Status() string {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+
+ switch c.started {
+ case true:
+ return "running"
+ default:
+ return "stopped"
}
- return true
-} \ No newline at end of file
+}