This commit is contained in:
Bruno Rybársky 2024-09-12 19:55:47 +02:00
parent e176c80db7
commit cf91fe9959
2 changed files with 87 additions and 40 deletions

100
main.go

@ -1,49 +1,69 @@
package main package main
import ( import (
"database/sql"
"encoding/json"
"fmt" "fmt"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"log" "math/rand"
"os" "net"
"time"
) )
func main() { // isPrivate checks if the IP address falls within private IP ranges
//resp, err := PingHostname("play.survival-games.cz", 25565) func isPrivate(ip [4]byte) bool {
resp, err := PingHostname("127.0.0.2", 25565) switch {
//resp, err := PingHostname("vps.brn.systems", 25965) case ip[0] == 10:
if err != nil { return true
fmt.Println("Ty debil") case ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31:
fmt.Println(err) return true
} case ip[0] == 192 && ip[1] == 168:
connectionBytes, err := os.ReadFile("secrets/.myconnectionstring") return true
if err != nil { default:
return return false
} }
db, err := sql.Open("mysql", string(connectionBytes)) }
if err != nil {
log.Fatal(err) var triedIPs []net.IP
}
defer func(db *sql.DB) { // generateRandomPublicIP generates a random public IP address
err := db.Close() func generateRandomPublicIP() net.IP {
if err != nil { rand.NewSource(time.Now().UnixNano())
log.Fatal(err) var ipTemp [4]byte
} var ip net.IP
}(db) for {
err = saveResponse(db, resp) ipTemp[0] = byte(rand.Intn(255)) + 1
if err != nil { ipTemp[1] = byte(rand.Intn(256))
log.Fatal(err) ipTemp[2] = byte(rand.Intn(256))
return ipTemp[3] = byte(rand.Intn(256))
}
// Pretty print the response ip = net.IPv4(ipTemp[0], ipTemp[1], ipTemp[2], ipTemp[3])
respJson, err := json.MarshalIndent(resp, "", " ")
if err != nil { // Ensure the generated IP is not in a private range
fmt.Println("Error marshalling response to JSON:", err) if !ip.IsPrivate() && !ip.IsLoopback() { // Exclude 0.x.x.x and 127.x.x.x (loopback)
return dupe := false
} for _, ipTried := range triedIPs {
err = os.WriteFile("out/server.json", respJson, 0644) if ipTried.Equal(ip) {
if err != nil { dupe = true
fmt.Println("Error creating server.json:", err) }
}
if !dupe {
break
}
}
}
return ip
}
func doIPScan() {
randomIP := generateRandomPublicIP()
_, err := PingIP(randomIP, 25565, "")
if err != nil {
fmt.Println(err)
}
}
func main() {
for i := 0; i < 100000000; i++ {
go doIPScan()
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
} }
} }

@ -2,13 +2,16 @@ package main
import ( import (
"bufio" "bufio"
"database/sql"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
"log"
"net" "net"
"os"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -34,6 +37,30 @@ func PingIP(ip net.IP, port uint16, host string) (response Response, errOut erro
_ = conn.Close() _ = conn.Close()
}(conn) }(conn)
defer func() {
if errOut == nil {
connectionBytes, err := os.ReadFile("secrets/.myconnectionstring")
if err != nil {
return
}
db, err := sql.Open("mysql", string(connectionBytes))
if err != nil {
log.Fatal(err)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatal(err)
}
}(db)
err = saveResponse(db, response)
if err != nil {
log.Fatal(err)
return
}
}
}()
var state byte var state byte
state = 0 state = 0
// 0 - handshaking // 0 - handshaking