-
-
Save colemancda/1ce4e3b07ea10a1044c3697074ee1881 to your computer and use it in GitHub Desktop.
Golang TinyMapleServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"encoding/binary" | |
"encoding/hex" | |
"flag" | |
"fmt" | |
"net" | |
"strconv" | |
) | |
var ( | |
version int | |
locale int | |
subversion string | |
port int | |
handshake []byte | |
appendedHex string | |
) | |
func handleConnection(con net.Conn) { | |
buffer := make([]byte, 1024) | |
writer := bufio.NewWriter(con) | |
reader := bufio.NewReader(con) | |
writer.Write(handshake) | |
writer.Flush() | |
// Keep reading | |
for { | |
if _, err := reader.Read(buffer); err != nil { | |
fmt.Println("Client exited or something, idk.") | |
return | |
} | |
} | |
} | |
func main() { | |
flag.IntVar(&version, "version", 0, "MapleStory version") | |
flag.StringVar(&subversion, "subversion", "", "MapleStory subversion") | |
flag.IntVar(&locale, "locale", 0, "MapleStory locale") | |
flag.StringVar(&appendedHex, "append-hex", "", "Additional bytes appended to the handshake (in HEX, without spaces)") | |
flag.IntVar(&port, "port", 8484, "Port to listen on") | |
flag.Parse() | |
fmt.Println("Hosting tiny MapleStory server on port", port, "for version", version, ", subversion", subversion, "and locale", locale) | |
buf := new(bytes.Buffer) | |
handshakeSize := 2 + 2 + len(subversion) + 4 + 4 + 1 | |
binary.Write(buf, binary.LittleEndian, uint16(handshakeSize)) | |
binary.Write(buf, binary.LittleEndian, uint16(version)) | |
binary.Write(buf, binary.LittleEndian, uint16(len(subversion))) | |
buf.Write([]byte(subversion)) | |
binary.Write(buf, binary.LittleEndian, uint32(0xBAADB00B)) | |
binary.Write(buf, binary.LittleEndian, uint32(0xDEADBEEF)) | |
binary.Write(buf, binary.LittleEndian, byte(locale)) | |
if hexBytes, err := hex.DecodeString(appendedHex); err != nil { | |
panic(err) | |
} else { | |
buf.Write(hexBytes) | |
} | |
handshake = buf.Bytes() | |
fmt.Println(hex.Dump(handshake)) | |
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port)) | |
if err != nil { | |
panic(err) | |
} | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
fmt.Println("Error while accepting client:", err) | |
} else { | |
fmt.Println("Got client!") | |
go handleConnection(conn) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment