summaryrefslogtreecommitdiff
path: root/webircgateway/pkg/identd/rpcclient.go
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-10-24 00:11:55 +0800
committerMistivia <i@mistivia.com>2025-10-24 00:11:55 +0800
commitffa71fb8b2e5521f93d8599279af2b28a0795a66 (patch)
treeacd96fd3e03cb39c8a648c6298b96b9c847e716f /webircgateway/pkg/identd/rpcclient.go
parent5c71d2a538a93fd4a4fc06cb7941231cc5c0b104 (diff)
add web irc
Diffstat (limited to 'webircgateway/pkg/identd/rpcclient.go')
-rw-r--r--webircgateway/pkg/identd/rpcclient.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/webircgateway/pkg/identd/rpcclient.go b/webircgateway/pkg/identd/rpcclient.go
new file mode 100644
index 0000000..37aec3e
--- /dev/null
+++ b/webircgateway/pkg/identd/rpcclient.go
@@ -0,0 +1,59 @@
+package identd
+
+import "net"
+import "fmt"
+import "time"
+
+func MakeRpcClient(appName string) *RpcClient {
+ return &RpcClient{AppName: appName}
+}
+
+type RpcClient struct {
+ AppName string
+ Conn *net.Conn
+}
+
+func (rpc *RpcClient) ConnectAndReconnect(serverAddress string) {
+ for {
+ if rpc.Conn == nil {
+ println("Connecting to identd RPC...")
+ rpc.Connect(serverAddress)
+ }
+
+ time.Sleep(time.Second * 3)
+ }
+}
+
+func (rpc *RpcClient) Connect(serverAddress string) error {
+ conn, err := net.Dial("tcp", serverAddress)
+ if err != nil {
+ return err
+ }
+
+ rpc.Conn = &conn
+ rpc.Write("id " + rpc.AppName)
+
+ return nil
+}
+
+func (rpc *RpcClient) Write(line string) error {
+ if rpc.Conn == nil {
+ return fmt.Errorf("not connected")
+ }
+
+ conn := *rpc.Conn
+ _, err := conn.Write([]byte(line + "\n"))
+ if err != nil {
+ rpc.Conn = nil
+ conn.Close()
+ }
+ return err
+}
+
+func (rpc *RpcClient) AddIdent(lport int, rport int, username string, iface string) {
+ rpc.Write(fmt.Sprintf("add %s %d %d %s", username, lport, rport, iface))
+}
+
+func (rpc *RpcClient) RemoveIdent(lport int, rport int, username string, iface string) {
+ rpc.Write(fmt.Sprintf("del %d %d %s", lport, rport, iface))
+}