Skip to content
Snippets Groups Projects
Verified Commit 9eaab22b authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Implemented first demo of tool for connection to quassel cores

parents
Branches
No related tags found
No related merge requests found
Pipeline #431 failed
*.iml
/.idea/*
!/.idea/copyright/
.DS_Store
\ No newline at end of file
main.go 0 → 100644
package main
import (
"bufio"
"crypto/tls"
"encoding/binary"
"fmt"
"net"
)
const PROTOCOL_MAGIC uint32 = 0x42b33f00
const PROTOCOL_FEATURE_TLS uint8 = 0x01
const PROTOCOL_FEATURE_COMPRESSION uint8 = 0x02
const PROTOCOL_LEGACY uint32 = 0x01
const PROTOCOL_DATASTREAM uint32 = 0x02
type connection struct {
hostname string
socket net.Conn
tlsSocket *tls.Conn
readWriter *bufio.ReadWriter
buffer []byte
}
func makeConnection(address string, port int) connection {
socket, err := net.Dial("tcp", fmt.Sprintf("%s:%d", address, port))
if err != nil {
panic(err.Error())
}
return connection{
hostname: address,
socket: socket,
readWriter: bufio.NewReadWriter(
bufio.NewReader(socket),
bufio.NewWriter(socket),
),
buffer: make([]byte, 4),
}
}
func (c *connection) write(data uint32) {
binary.BigEndian.PutUint32(c.buffer, data)
_, err := c.readWriter.Write(c.buffer)
if err != nil {
panic(err.Error())
}
}
func (c *connection) read(len int) []byte {
buffer := make([]byte, len)
_, err := c.readWriter.Read(buffer)
if err != nil {
panic(err.Error())
}
return buffer
}
func (c *connection) flush() {
err := c.readWriter.Flush()
if err != nil {
panic(err.Error())
}
}
func (c *connection) withTLS(verify bool) {
config := &tls.Config{
ServerName: c.hostname,
InsecureSkipVerify: !verify,
}
c.tlsSocket = tls.Client(c.socket, config)
c.readWriter = bufio.NewReadWriter(
bufio.NewReader(c.tlsSocket),
bufio.NewWriter(c.tlsSocket),
)
err := c.tlsSocket.Handshake()
if err != nil {
panic(err.Error())
}
}
func (c *connection) tlsState() *tls.ConnectionState {
if c.tlsSocket != nil {
state := c.tlsSocket.ConnectionState()
return &state
} else {
return nil
}
}
func (c *connection) close() {
_ = c.readWriter.Flush()
_ = c.tlsSocket.Close()
_ = c.socket.Close()
}
type protocolInfo struct {
flagTLS bool
flagCompression bool
data uint16
version uint8
}
func parseProtocolInfo(data []byte) protocolInfo {
rawFeatures := data[0]
rawData := data[1:3]
rawVersion := data[3]
return protocolInfo{
rawFeatures&PROTOCOL_FEATURE_TLS != 0,
rawFeatures&PROTOCOL_FEATURE_COMPRESSION != 0,
binary.BigEndian.Uint16(rawData),
rawVersion,
}
}
func main() {
conn := makeConnection("kuschku.de", 4242)
conn.write(PROTOCOL_MAGIC | uint32(PROTOCOL_FEATURE_TLS))
supportedProtocols := []uint32{
PROTOCOL_DATASTREAM,
}
for _, protocol := range supportedProtocols {
conn.write(protocol)
}
conn.write(1 << 31)
conn.flush()
protocolInfo := parseProtocolInfo(conn.read(4))
println("Read Protocol Info")
if protocolInfo.flagTLS {
conn.withTLS(false)
}
if state := conn.tlsState(); state != nil {
for _, cert := range state.PeerCertificates {
println(cert.NotAfter.String())
}
}
conn.close()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment