summaryrefslogtreecommitdiff
path: root/deprecated-webircgateway/pkg/identd/rpcclient.go
blob: 37aec3ea4b677fea653d37becb0f064b14d88580 (plain)
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
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))
}