Created
May 1, 2015 14:24
-
-
Save stroum/863e92f36accb926c8c1 to your computer and use it in GitHub Desktop.
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
/* | |
The MIT License (MIT) | |
Copyright (c) <year> <copyright holders> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
package main | |
import ( | |
"bytes" | |
"fmt" | |
"net" | |
"flag" | |
"log" | |
"encoding/hex" | |
"encoding/binary" | |
"regexp" | |
) | |
func hex_str(text string) string { | |
var buffer bytes.Buffer | |
for i := 0; i < len(text); i++ { | |
x := fmt.Sprintf("\\x%x", text[i]) | |
buffer.WriteString(x) | |
} | |
return buffer.String() | |
} | |
func pck_byte(x uint32) string { | |
buf := new(bytes.Buffer) | |
err := binary.Write(buf, binary.LittleEndian, x) | |
if err != nil { | |
fmt.Println("binary.Write failed:", err) | |
} | |
v := fmt.Sprintf("%x", x) | |
p, err := hex.DecodeString(v) | |
return string(p) | |
} | |
func pck_float(x float32) string { | |
buf := new(bytes.Buffer) | |
err := binary.Write(buf, binary.LittleEndian, x) | |
if err != nil { | |
fmt.Println("binary.Write failed:", err) | |
} | |
return buf.String() | |
} | |
func main() { | |
log.Println("Listening on :27020") | |
server_name := flag.String("name", "[DM] Stroum Server", "Server name") | |
server_map := flag.String("map", "de_dust2", "Server map") | |
redirect_serv := flag.String("redirect", "37.143.13.73", "Redirect to") | |
flag.Parse() | |
players := pck_byte(20) | |
max_players := pck_byte(32) | |
log.Printf("\"%s\"", pck_float(5000)) | |
server_info := "\xff\xff\xff\xff\x49\x30" + *server_name + "\x00" + *server_map + | |
"\x00\x63\x73\x74\x72\x69\x6b\x65\x00\x43\x6f\x75\x6e\x74\x65\x72\x2d\x53\x74\x72\x69\x6b\x65\x00\x0a\x00" + players + | |
max_players + "\x00\x64\x6c\x00\x01" | |
l, err := net.ResolveUDPAddr("udp",":27020") | |
if err != nil { | |
log.Println("Error:", err) | |
} | |
sc, err := net.ListenUDP("udp", l) | |
if err != nil { | |
log.Println("Error:", err) | |
} | |
defer sc.Close() | |
buf := make([]byte, 1024) | |
for { | |
n, addr, err := sc.ReadFromUDP(buf) | |
msg := string(buf[0:n]) | |
dump := hex.Dump([]byte(msg)) | |
//log.Println("[", addr, "] ", msg) | |
if msg == "\xff\xff\xff\xff\x54\x53\x6f\x75\x72\x63\x65\x20\x45\x6e\x67\x69\x6e\x65\x20\x51\x75\x65\x72\x79\x00" { | |
xyu, err := sc.WriteTo([]byte(server_info), addr) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
fmt.Println(xyu) | |
} | |
if msg == "\xff\xff\xff\xff\x67\x65\x74\x63\x68\x61\x6c\x6c\x65\x6e\x67\x65\x20\x73\x74\x65\x61\x6d\x0a" { | |
xyu, err := sc.WriteTo([]byte("\xFF\xFF\xFF\xFF\x41\x30\x30\x30\x30\x30\x30\x30\x30\x201859943975\x20\x32\x0A\x00"), addr) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
fmt.Println(xyu) | |
} | |
if msg == "\xFF\xFF\xFF\xFF\x69" { | |
xyu, err := sc.WriteTo([]byte("\xFF\xFF\xFF\xFF\x6A\x00"), addr) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
fmt.Println(xyu) | |
} | |
if msg == "\xff\xff\xff\xff\x55\x00\x00\x00\x00" { | |
//resp := "\xFF\xFF\xFF\xFF\x44\x02\x01\x5B\x44\x5D\x2D\x2D\x2D\x2D\x3E\x54\x2E\x4E\x2E\x57\x3C\x2D\x2D\x2D\x2D\x00\x0E\x00\x00\x00\xB4\x97\x00\x44\x02\x4B\x69\x6C\x6C\x65\x72\x20\x21\x21\x21\x00\x05\x00\x00\x00\x69\x24\xD9\x43" | |
resp := "\xFF\xFF\xFF\xFF\x44" | |
resp += pck_byte(3) | |
// pck_byte(0) + pck_string("uptime") + pck_long(100) + pck_float(uptime) | |
resp += pck_byte(0) + "stroum\x00" + "10L\x00" + pck_float(600) | |
xyu, err := sc.WriteTo([]byte(resp), addr) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
fmt.Println(xyu) | |
} | |
re := regexp.MustCompile(`connect`) | |
match := re.FindStringSubmatch(msg) | |
if len(match) != 0 { | |
xyu, err := sc.WriteTo([]byte("\xFF\xFF\xFF\xFFL " + *redirect_serv), addr) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
fmt.Println(xyu) | |
} | |
fmt.Println(dump) | |
//fmt.Println("Received ", " from ",addr) | |
if err != nil { | |
fmt.Println("Error:", err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment