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

92
main.go

@ -1,49 +1,69 @@
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"os"
"math/rand"
"net"
"time"
)
func main() {
//resp, err := PingHostname("play.survival-games.cz", 25565)
resp, err := PingHostname("127.0.0.2", 25565)
//resp, err := PingHostname("vps.brn.systems", 25965)
// isPrivate checks if the IP address falls within private IP ranges
func isPrivate(ip [4]byte) bool {
switch {
case ip[0] == 10:
return true
case ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31:
return true
case ip[0] == 192 && ip[1] == 168:
return true
default:
return false
}
}
var triedIPs []net.IP
// generateRandomPublicIP generates a random public IP address
func generateRandomPublicIP() net.IP {
rand.NewSource(time.Now().UnixNano())
var ipTemp [4]byte
var ip net.IP
for {
ipTemp[0] = byte(rand.Intn(255)) + 1
ipTemp[1] = byte(rand.Intn(256))
ipTemp[2] = byte(rand.Intn(256))
ipTemp[3] = byte(rand.Intn(256))
ip = net.IPv4(ipTemp[0], ipTemp[1], ipTemp[2], ipTemp[3])
// Ensure the generated IP is not in a private range
if !ip.IsPrivate() && !ip.IsLoopback() { // Exclude 0.x.x.x and 127.x.x.x (loopback)
dupe := false
for _, ipTried := range triedIPs {
if ipTried.Equal(ip) {
dupe = true
}
}
if !dupe {
break
}
}
}
return ip
}
func doIPScan() {
randomIP := generateRandomPublicIP()
_, err := PingIP(randomIP, 25565, "")
if err != nil {
fmt.Println("Ty debil")
fmt.Println(err)
}
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, resp)
if err != nil {
log.Fatal(err)
return
}
// Pretty print the response
respJson, err := json.MarshalIndent(resp, "", " ")
if err != nil {
fmt.Println("Error marshalling response to JSON:", err)
return
}
err = os.WriteFile("out/server.json", respJson, 0644)
if err != nil {
fmt.Println("Error creating server.json:", 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 (
"bufio"
"database/sql"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"log"
"net"
"os"
"reflect"
"strconv"
"strings"
@ -34,6 +37,30 @@ func PingIP(ip net.IP, port uint16, host string) (response Response, errOut erro
_ = conn.Close()
}(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
state = 0
// 0 - handshaking