/
exchange.go
136 lines (121 loc) · 4.14 KB
/
exchange.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cmd
import (
"fmt"
"github.com/4everlynn/journal/config"
"github.com/4everlynn/journal/support"
"github.com/gookit/color"
"github.com/spf13/cobra"
"net"
"regexp"
"strings"
)
func SysMsg(msg string) {
fmt.Printf("\r\n\033[s\033[1A\033[K\033[u%s%s\n",
color.FgGray.Render("Journal <- "),
color.FgGray.Render(msg))
}
func ReceivedMsg(msg string) {
fmt.Printf("\r\n\033[s\033[1A\033[K\033[u%s\n",
color.FgGreen.Render(msg))
}
func UserMsg(msg string) {
fmt.Printf("\033[s\033[1A\033[K\033[u%s\n", msg)
}
// exchangeCmd represents the exchange command
var exchangeCmd = &cobra.Command{
Use: "exchange",
Short: "Establish lan connection",
Long: `If you want to initiate a communication, you need to start it as a server,
and other people will establish a connection with you as a client.
If you only need to connect, run it as a client.`,
Run: func(cmd *cobra.Command, args []string) {
isServer, _ := cmd.Flags().GetBool("server")
isClient, _ := cmd.Flags().GetBool("client")
port, _ := cmd.Flags().GetInt("port")
host, _ := cmd.Flags().GetString("host")
name, _ := cmd.Flags().GetString("name")
if strings.Trim(name, " ") == "" {
fmt.Println(color.FgRed.Render("journal error: server/client needs name"))
return
}
properties := config.Exchange{
Port: port,
Host: host,
Protocol: "tcp",
Name: name,
}
if isClient {
support.Client(properties, support.ClientHooks{
Started: func() {
fmt.Printf("\033c")
},
Ready: func(ctx support.ClientContext) {
for {
print(color.FgWhite.Render(fmt.Sprintf("%s# ", ctx.Property.Name)))
words := ctx.Scanner.ReadLine()
ctx.Write([]byte(ctx.Property.Name + " <- " + words))
UserMsg(color.FgYellow.Render(ctx.Property.Name+" -> ") + color.FgYellow.Render(words))
}
},
Received: func(bytes []byte) {
ReceivedMsg(fmt.Sprintf("%s", string(bytes)))
print(color.FgWhite.Render(fmt.Sprintf("%s# ", properties.Name)))
},
})
} else if isServer {
support.Bootstrap(properties, support.ServerHooks{
Started: func(ctx support.ServerContext) {
//fmt.Printf("\033c")
},
Ready: func(ctx support.ServerContext) {
for {
print(color.FgWhite.Render(fmt.Sprintf("%s$ ", ctx.Property.Name)))
words := ctx.Scanner.ReadLine()
for {
parse, err := support.SmartFuncParser{}.Parse(words)
if err == nil {
invoke := config.Invoke(parse.Name, parse.Args...)
compile, err := regexp.Compile("@(\\w+)\\((.*?)\\)+")
support.Catch(err, func() {
match := compile.FindStringSubmatch(words)
words = strings.ReplaceAll(words, match[0], invoke)
})
} else {
break
}
}
println()
ctx.Write([]byte(ctx.Property.Name + " <- " + words))
UserMsg(color.FgYellow.Render(ctx.Property.Name+" -> ") + color.FgYellow.Render(words))
}
},
Deleted: func(client net.Conn, ctx support.ServerContext) {
SysMsg("IP " + client.RemoteAddr().String() + " LEAVE")
},
Received: func(bytes []byte, ctx support.ServerContext, conn net.Conn) {
ReceivedMsg(fmt.Sprintf("%s", string(bytes)))
// dispatch msg to other clients
for _, client := range ctx.Connections {
if client.RemoteAddr().String() != conn.RemoteAddr().String() {
_, _ = client.Write(bytes)
}
}
print(color.FgWhite.Render(fmt.Sprintf("%s$ ", properties.Name)))
},
Accepted: func(client net.Conn, ctx support.ServerContext) {
SysMsg("IP " + client.RemoteAddr().String() + " CONNECTED")
print(color.FgWhite.Render(fmt.Sprintf("%s$ ", ctx.Property.Name)))
},
})
}
},
}
func init() {
rootCmd.AddCommand(exchangeCmd)
exchangeCmd.Flags().BoolP("server", "s", true, "run as server")
exchangeCmd.Flags().BoolP("client", "c", false, "run as client")
exchangeCmd.Flags().StringP("host", "H", "localhost", "specify server/client host address")
exchangeCmd.Flags().IntP("port", "p", 9651, "specify server/client port")
exchangeCmd.Flags().StringP("name", "n", "", "specify server/client temporary name")
exchangeCmd.Flags().StringP("auth", "a", "", "specify connection authorization code")
}