Skip to content

Commit 3564f70

Browse files
author
Arslan Ametov
committed
cobra cli lib added
1 parent a4aa5f8 commit 3564f70

24 files changed

+8014
-86
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xpos

broker/main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func main() {
13-
ln, err := net.Listen("tcp", ":4321")
13+
ln, err := net.Listen("tcp4", "0.0.0.0:4321")
1414
if err != nil {
1515
log.Fatal(err)
1616
}
@@ -44,8 +44,9 @@ func processConn(conn net.Conn) {
4444

4545
tunnelCreatedEvent := &events.Event[events.TunnelCreated]{
4646
Data: &events.TunnelCreated{
47-
PublicAddr: tunnel.PublicLn(),
48-
PrivateAddr: tunnel.PrivateLn(),
47+
Hostname: "34.229.0.117",
48+
PublicListenerPort: tunnel.PublicLn(),
49+
PrivateListenerPort: tunnel.PrivateLn(),
4950
},
5051
}
5152

broker/tunnel/tunnel.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"log"
66
"net"
7+
"strconv"
78
"sync"
89

910
"github.com/ametow/xpos/events"
@@ -33,16 +34,16 @@ func (tn *Tunnel) PublicLn() string {
3334
}
3435

3536
func (tn *Tunnel) Init() {
36-
pubLn, err := net.Listen("tcp", "127.0.0.1:")
37+
pubLn, err := net.Listen("tcp", "0.0.0.0:")
3738
if err != nil {
3839
log.Fatal(err)
3940
}
40-
privLn, err := net.Listen("tcp", "127.0.0.1:")
41+
privLn, err := net.Listen("tcp", "0.0.0.0:")
4142
if err != nil {
4243
log.Fatal(err)
4344
}
44-
tn.privateAddr = privLn.Addr().(*net.TCPAddr).AddrPort().String()
45-
tn.publicAddr = pubLn.Addr().(*net.TCPAddr).AddrPort().String()
45+
tn.privateAddr = strconv.Itoa(privLn.Addr().(*net.TCPAddr).Port)
46+
tn.publicAddr = strconv.Itoa(pubLn.Addr().(*net.TCPAddr).Port)
4647

4748
go processListener(privLn, tn.privConnHandler)
4849
go processListener(pubLn, tn.publicConnHandler)

cli/cmd/root.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
homedir "github.com/mitchellh/go-homedir"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
var (
13+
userLicense = ""
14+
cfgFile = ""
15+
)
16+
17+
var rootCmd = &cobra.Command{
18+
Use: "xpos",
19+
Short: "Xpos is coolest lib",
20+
Long: `Xpos is coolest lib`,
21+
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
22+
Run: func(cmd *cobra.Command, args []string) {
23+
tcpCommand.Usage()
24+
},
25+
}
26+
27+
func Execute() {
28+
if err := rootCmd.Execute(); err != nil {
29+
fmt.Println(err)
30+
os.Exit(1)
31+
}
32+
}
33+
34+
func init() {
35+
// cobra.OnInitialize(initConfig)
36+
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "xpos", "config file (default is $HOME/.xpos.yaml)")
37+
// viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
38+
}
39+
40+
func initConfig() {
41+
// Don't forget to read config either from cfgFile or from home directory!
42+
if cfgFile != "" {
43+
// Use config file from the flag.
44+
viper.SetConfigFile(cfgFile)
45+
} else {
46+
// Find home directory.
47+
home, err := homedir.Dir()
48+
if err != nil {
49+
fmt.Println(err)
50+
os.Exit(1)
51+
}
52+
53+
viper.AddConfigPath(home)
54+
viper.SetConfigName(".xpos")
55+
}
56+
57+
if err := viper.ReadInConfig(); err != nil {
58+
fmt.Println("Can't read config:", err)
59+
os.Exit(1)
60+
}
61+
}

cli/cmd/tcp.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cmd
2+
3+
import (
4+
"encoding/binary"
5+
"fmt"
6+
"log"
7+
"net"
8+
9+
"github.com/ametow/xpos/events"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
const BASEURL = "34.229.0.117:4321"
14+
15+
func init() {
16+
rootCmd.AddCommand(tcpCommand)
17+
}
18+
19+
var tcpCommand = &cobra.Command{
20+
Use: "tcp [port]",
21+
Short: "Forward tcp traffic",
22+
Long: `All software has versions.`,
23+
Args: cobra.ExactArgs(1),
24+
Run: func(cmd *cobra.Command, args []string) {
25+
LocalAddr = "127.0.0.1:" + args[0]
26+
27+
conn, err := net.Dial("tcp", BASEURL)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
defer conn.Close()
32+
33+
request := &events.Event[events.TunnelRequest]{
34+
Data: &events.TunnelRequest{},
35+
}
36+
err = request.Write(conn)
37+
if err != nil {
38+
log.Fatal("error requesting tunnel:", err)
39+
}
40+
41+
tunnedCreated := &events.Event[events.TunnelCreated]{
42+
Data: &events.TunnelCreated{},
43+
}
44+
err = tunnedCreated.Read(conn)
45+
if err != nil {
46+
log.Fatal("error creating tunnel:", err)
47+
}
48+
49+
fmt.Println("Started listening on public network.")
50+
fmt.Printf("Public addr: http://%s:%s\n", tunnedCreated.Data.Hostname, tunnedCreated.Data.PublicListenerPort)
51+
fmt.Printf("Private addr: http://%s:%s\n", tunnedCreated.Data.Hostname, tunnedCreated.Data.PrivateListenerPort)
52+
PrivateAddr = net.JoinHostPort(tunnedCreated.Data.Hostname, tunnedCreated.Data.PrivateListenerPort)
53+
54+
for {
55+
newConnectionEvent := &events.Event[events.NewConnection]{Data: &events.NewConnection{}}
56+
err := newConnectionEvent.Read(conn)
57+
if err != nil {
58+
log.Fatal("error on new connection receive: ", err)
59+
}
60+
61+
go handleConn(newConnectionEvent)
62+
}
63+
},
64+
}
65+
66+
var PrivateAddr string
67+
var LocalAddr string
68+
69+
func handleConn(client *events.Event[events.NewConnection]) {
70+
// local dial
71+
localConn, err := net.Dial("tcp", LocalAddr)
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
// remote dial
76+
remoteConn, err := net.Dial("tcp4", PrivateAddr)
77+
if err != nil {
78+
log.Fatal(err)
79+
}
80+
81+
buf := make([]byte, 2)
82+
binary.LittleEndian.PutUint16(buf, uint16(client.Data.ClientPort))
83+
84+
_, err = remoteConn.Write(buf)
85+
if err != nil {
86+
log.Fatal(err)
87+
}
88+
89+
go events.Bind(localConn, remoteConn)
90+
events.Bind(remoteConn, localConn)
91+
}

cli/cmd/version.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func init() {
10+
rootCmd.AddCommand(versionCmd)
11+
}
12+
13+
var versionCmd = &cobra.Command{
14+
Use: "version",
15+
Short: "Print the version number of xpos",
16+
Long: `All software has versions. This is xpos's`,
17+
Run: func(cmd *cobra.Command, args []string) {
18+
fmt.Println("xpos proxy v0.9 -- HEAD")
19+
},
20+
}

cli/main.go

+2-75
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,7 @@
11
package main
22

3-
import (
4-
"encoding/binary"
5-
"flag"
6-
"fmt"
7-
"log"
8-
"net"
9-
10-
"github.com/ametow/xpos/events"
11-
)
12-
13-
const BASEURL = "localhost:4321"
14-
15-
var PrivateAddr string
16-
var LocalAddr string = "localhost:7777"
3+
import "github.com/ametow/xpos/cli/cmd"
174

185
func main() {
19-
flag.Parse()
20-
21-
conn, err := net.Dial("tcp", BASEURL)
22-
if err != nil {
23-
panic(err)
24-
}
25-
26-
request := &events.Event[events.TunnelRequest]{
27-
Data: &events.TunnelRequest{},
28-
}
29-
err = request.Write(conn)
30-
if err != nil {
31-
log.Fatal("error requesting tunnel:", err)
32-
}
33-
34-
tunnedCreated := &events.Event[events.TunnelCreated]{
35-
Data: &events.TunnelCreated{},
36-
}
37-
err = tunnedCreated.Read(conn)
38-
if err != nil {
39-
log.Fatal("error creating tunnel:", err)
40-
}
41-
42-
fmt.Println("Started listening on public network.")
43-
fmt.Println("Public addr: ", tunnedCreated.Data.PublicAddr)
44-
fmt.Println("Private addr: ", tunnedCreated.Data.PrivateAddr)
45-
PrivateAddr = tunnedCreated.Data.PrivateAddr
46-
47-
for {
48-
newconn := &events.Event[events.NewConnection]{Data: &events.NewConnection{}}
49-
err := newconn.Read(conn)
50-
if err != nil {
51-
log.Fatal("error on new connection receive: ", err)
52-
}
53-
54-
go handleConn(newconn)
55-
}
56-
}
57-
58-
func handleConn(client *events.Event[events.NewConnection]) {
59-
// local dial
60-
localConn, err := net.Dial("tcp", LocalAddr)
61-
if err != nil {
62-
log.Fatal(err)
63-
}
64-
// remote dial
65-
remoteConn, err := net.Dial("tcp", PrivateAddr)
66-
if err != nil {
67-
log.Fatal(err)
68-
}
69-
70-
buf := make([]byte, 2)
71-
binary.LittleEndian.PutUint16(buf, uint16(client.Data.ClientPort))
72-
73-
_, err = remoteConn.Write(buf)
74-
if err != nil {
75-
log.Fatal(err)
76-
}
77-
78-
go events.Bind(localConn, remoteConn)
79-
events.Bind(remoteConn, localConn)
6+
cmd.Execute()
807
}

etc/config.yaml

Whitespace-only changes.

events/events.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ type TunnelRequest struct {
1717
}
1818

1919
type TunnelCreated struct {
20-
PublicAddr string
21-
PrivateAddr string
20+
Hostname string
21+
PublicListenerPort string
22+
PrivateListenerPort string
2223
}
2324

2425
type NewConnection struct {
@@ -75,12 +76,12 @@ func Bind(src net.Conn, dst net.Conn) error {
7576
defer dst.Close()
7677
buf := make([]byte, 4096)
7778
for {
78-
_ = src.SetReadDeadline(time.Now().Add(time.Second))
79+
_ = src.SetReadDeadline(time.Now().Add(time.Second * 10))
7980
n, err := src.Read(buf)
8081
if err == io.EOF {
8182
break
8283
}
83-
_ = dst.SetWriteDeadline(time.Now().Add(time.Second))
84+
_ = dst.SetWriteDeadline(time.Now().Add(time.Second * 10))
8485
_, err = dst.Write(buf[:n])
8586
if err != nil {
8687
return err

0 commit comments

Comments
 (0)