Init commit

This commit is contained in:
2020-06-19 15:31:20 +02:00
commit ed6baf7753
9 changed files with 521 additions and 0 deletions

3
net/const.go Normal file
View File

@@ -0,0 +1,3 @@
package net
const ServerPort = 1337

72
net/listener.go Normal file
View File

@@ -0,0 +1,72 @@
package net
import (
"bufio"
"flag"
"fmt"
"github.com/maride/afl-transmit/logistic"
"io"
"log"
"net"
"strings"
)
var (
port int
)
// Registers the flags required for the listener
func RegisterListenFlags() {
flag.IntVar(&port, "port", ServerPort, "Port to bind server component to")
}
// Sets up a listener and listens forever for packets on the given port, storing their contents in the outputDirectory
func Listen(outputDirectory string) error {
// Create listener
addrStr := fmt.Sprintf(":%v", port)
listener, listenErr := net.Listen("tcp", addrStr)
if listenErr != nil {
return listenErr
}
// Prepare output directory path
outputDirectory = strings.TrimRight(outputDirectory, "/")
// Listen forever
for {
// Accept connection
conn, connErr := listener.Accept()
if connErr != nil {
log.Printf("Encountered error while accepting from %s: %s", conn.RemoteAddr().String(), connErr)
continue
}
// Handle in a separate thread
go handle(conn, outputDirectory)
}
}
// Handles a single connection, and unpacks the received data into outputDirectory
func handle(conn net.Conn, outputDirectory string) {
// Make sure to close connection on return
defer conn.Close()
// Loop until we either hit EOF or an error
for {
// Read raw content
cont, contErr := bufio.NewReader(conn).ReadString('\x00')
if contErr == io.EOF {
// We received the whole content, time to process it
unpackErr := logistic.UnpackInto([]byte(cont), outputDirectory)
if unpackErr != nil {
log.Printf("Encountered error processing packet from %s: %s", conn.RemoteAddr().String(), unpackErr)
}
return
} else if contErr != nil {
// We encountered an error on that connection
log.Printf("Encountered error while reading from %s: %s", conn.RemoteAddr().String(), contErr)
return
}
}
}

30
net/peer.go Normal file
View File

@@ -0,0 +1,30 @@
package net
import (
"log"
"net"
)
type Peer struct {
Address string
}
// Sends the given content to the peer
func (p *Peer) SendToPeer(content []byte) {
// Build up a connection
tcpConn, dialErr := net.Dial("tcp", p.Address)
if dialErr != nil {
log.Printf("Unable to connect to peer %s: %s", p.Address, dialErr)
return
}
// Send
_, writeErr := tcpConn.Write(content)
if writeErr != nil {
log.Printf("Unable to write to peer %s: %s", tcpConn.RemoteAddr().String(), writeErr)
return
}
// Close connection
tcpConn.Close()
}

96
net/sender.go Normal file
View File

@@ -0,0 +1,96 @@
package net
import (
"flag"
"io/ioutil"
"log"
"strings"
)
var (
peers []Peer
peerFile string
peerString string
)
// Registers flags required for peer parsing
func RegisterSenderFlags() {
flag.StringVar(&peerFile, "peersFile", "", "File which contains the addresses for all peers, one per line")
flag.StringVar(&peerString, "peers", "", "Addresses to peers, comma-separated.")
}
// Send the given content to all peers
func SendToPeers(content []byte) {
for _, p := range peers {
p.SendToPeer(content)
}
}
// Parses both peerString and peerFile, and adds all the peers to an internal array.
func ReadPeers() {
// Read peer file if it is given
if peerFile != "" {
fileErr := readPeersFile(peerFile)
// Check if we encountered errors
if fileErr != nil {
log.Printf("Failed to read peer file: %s", fileErr)
}
}
// Read peer string if it is given
if peerString != "" {
readPeersString(peerString)
}
// Remove doubles.
removeDoubledPeers()
log.Printf("Configured %d unique peers.", len(peers))
}
// Read a peer file at the given path, parses it and adds newly created Peers to the internal peers array
func readPeersFile(path string) error {
// Read file
readContBytes, readErr := ioutil.ReadFile(path)
if readErr != nil {
return readErr
}
// Convert to string
readCont := string(readContBytes)
// Iterate over it, line by line
for _, line := range strings.Split(readCont, "\n") {
// Append newly created peer to array
peers = append(peers, Peer{
Address: line,
})
}
return nil
}
// Read peers from the given string, parses it and adds newly created Peers to the internal peers array
func readPeersString(raw string) {
for _, peer := range strings.Split(raw, ",") {
// Append newly created peer to array
peers = append(peers, Peer{
Address: strings.TrimSpace(peer),
})
}
}
// Iterates over the peers array and removes doubles
func removeDoubledPeers() {
// Outer loop - go over all peers
for i := 0; i < len(peers); i++ {
// Inner loop - go over peers after the current (i) one, removing those with the same address
for j := i + 1; j < len(peers); j++ {
if peers[j].Address == peers[i].Address {
// Double found, remove j'th element
peers = append(peers[:j], peers[j+1:]...)
}
}
}
}