mirror of
https://github.com/IllusionMan1212/gorc.git
synced 2026-10-24 02:12:33 +00:00
feat: allow insecure tcp connections
This commit is contained in:
+40
-11
@@ -20,12 +20,16 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
// tls connection
|
||||||
|
TlsConn *tls.Conn
|
||||||
|
|
||||||
// tcp connection
|
// tcp connection
|
||||||
Conn *tls.Conn
|
TcpConn *net.TCPConn
|
||||||
}
|
}
|
||||||
|
|
||||||
const CRLF = "\r\n"
|
const CRLF = "\r\n"
|
||||||
@@ -34,24 +38,37 @@ func NewClient(host string, port string, tlsEnabled bool) *Client {
|
|||||||
// TODO: distinguish between secure and insecure connections
|
// TODO: distinguish between secure and insecure connections
|
||||||
addr := fmt.Sprintf("%s:%s", host, port)
|
addr := fmt.Sprintf("%s:%s", host, port)
|
||||||
|
|
||||||
cfg := &tls.Config{ServerName: host}
|
if tlsEnabled {
|
||||||
conn, err := tls.Dial("tcp", addr, cfg)
|
cfg := &tls.Config{ServerName: host}
|
||||||
|
conn, err := tls.Dial("tcp", addr, cfg)
|
||||||
|
if err != nil {
|
||||||
|
// TODO: properly handle the error instead of Fatal-ing (failed to initiate a connection to server)
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Client{
|
||||||
|
TlsConn: conn,
|
||||||
|
TcpConn: nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addrTCP, err := net.ResolveTCPAddr("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.DialTCP("tcp", nil, addrTCP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: properly handle the error instead of Fatal-ing (failed to initiate a connection to server)
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
Conn: conn,
|
TlsConn: nil,
|
||||||
|
TcpConn: conn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) Register(nick string, password string, channel string) {
|
func (c Client) Register(nick string, password string, channel string) {
|
||||||
if c.Conn == nil {
|
|
||||||
// TODO: properly handle the error instead of Fatal-ing
|
|
||||||
log.Fatal("Attempted to write data to nil connection")
|
|
||||||
}
|
|
||||||
|
|
||||||
c.SendCommand("CAP", "LS")
|
c.SendCommand("CAP", "LS")
|
||||||
if password != "" {
|
if password != "" {
|
||||||
c.SendCommand("PASS", password)
|
c.SendCommand("PASS", password)
|
||||||
@@ -65,6 +82,11 @@ func (c Client) Register(nick string, password string, channel string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) SendCommand(cmd string, params ...string) {
|
func (c Client) SendCommand(cmd string, params ...string) {
|
||||||
|
if c.TlsConn == nil && c.TcpConn == nil {
|
||||||
|
// TODO: properly handle the error instead of Fatal-ing
|
||||||
|
log.Fatal("Attempted to write data to nil connection")
|
||||||
|
}
|
||||||
|
|
||||||
paramsString := ""
|
paramsString := ""
|
||||||
if len(params) > 0 {
|
if len(params) > 0 {
|
||||||
paramsString = " " + strings.Join(params, " ")
|
paramsString = " " + strings.Join(params, " ")
|
||||||
@@ -79,5 +101,12 @@ func (c Client) SendCommand(cmd string, params ...string) {
|
|||||||
paramsString = ":" + paramsString
|
paramsString = ":" + paramsString
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Conn.Write([]byte(cmd + paramsString + CRLF))
|
if c.TlsConn != nil {
|
||||||
|
c.TlsConn.Write([]byte(cmd + paramsString + CRLF))
|
||||||
|
} else if c.TcpConn != nil {
|
||||||
|
c.TcpConn.Write([]byte(cmd + paramsString + CRLF))
|
||||||
|
} else {
|
||||||
|
// TODO: properly handle the error instead of Fatal-ing
|
||||||
|
log.Fatal("No valid connections to write to")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-4
@@ -83,8 +83,11 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c":
|
case "ctrl+c":
|
||||||
// TODO: clean up and gracefully quit by leaving the IRC channel/server and then quit bubble.
|
// TODO: clean up and gracefully quit by leaving the IRC channel/server and then quit bubble.
|
||||||
if s.client.Conn != nil {
|
if s.client.TlsConn != nil {
|
||||||
s.client.Conn.Close()
|
s.client.TlsConn.Close()
|
||||||
|
}
|
||||||
|
if s.client.TcpConn != nil {
|
||||||
|
s.client.TcpConn.Close()
|
||||||
}
|
}
|
||||||
return s, tea.Quit
|
return s, tea.Quit
|
||||||
}
|
}
|
||||||
@@ -114,8 +117,13 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
s.client = client
|
s.client = client
|
||||||
s.ui.mainScreen.Client = client
|
s.ui.mainScreen.Client = client
|
||||||
|
|
||||||
r := bufio.NewReaderSize(client.Conn, 512)
|
if tlsEnabled {
|
||||||
s.ui.mainScreen.Reader = r
|
r := bufio.NewReaderSize(client.TlsConn, 512)
|
||||||
|
s.ui.mainScreen.Reader = r
|
||||||
|
} else {
|
||||||
|
r := bufio.NewReaderSize(client.TcpConn, 512)
|
||||||
|
s.ui.mainScreen.Reader = r
|
||||||
|
}
|
||||||
|
|
||||||
return s, mainscreen.InitialRead
|
return s, mainscreen.InitialRead
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user