Skip to content
Snippets Groups Projects
Verified Commit 333ab2fc authored by Tóth Miklós Tibor's avatar Tóth Miklós Tibor :shrug:
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ppscrape.iml" filepath="$PROJECT_DIR$/.idea/ppscrape.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
go.mod 0 → 100644
module ppscrape
go 1.20
require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/samber/lo v1.38.1 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
)
last 0 → 100644
122528
\ No newline at end of file
main.go 0 → 100644
package main
import (
"fmt"
"github.com/gorilla/websocket"
"github.com/samber/lo"
"net"
"os"
"ppscrape/tutter"
"regexp"
"strings"
)
var socket = "ws://127.0.0.1:8080/ws"
func init() {
_socket := os.Getenv("SOCKET")
if _socket != "" {
socket = _socket
}
}
type Discovered struct {
Type string `json:"type"`
Id int `json:"id"`
IPs []string `json:"ips"`
}
const cookiecutter = `#important #addr
New IPs just dropped! Get them while they're hot!`
func main() {
c, _, e := websocket.DefaultDialer.Dial(socket, nil)
if e != nil {
panic(e)
}
for m := range tutter.LongPoll() {
if m.Author.Name == "ollescram" {
m.Text = string(lo.Reverse([]byte(m.Text)))
}
if strings.HasPrefix(m.Text, cookiecutter) {
continue
}
ips := ExtractIPv6Addresses(m.Text)
if len(ips) > 0 {
fmt.Println(m.Text)
_ = c.WriteJSON(Discovered{
Type: "discovered",
Id: 0123,
IPs: ips,
})
}
}
}
// ExtractIPv6Addresses extracts all possible IPv6 addresses from a string.
func ExtractIPv6Addresses(s string) []string {
// IPv6 addresses are typically represented in eight groups of four hexadecimal digits each, separated by colons.
// This regex will capture possible IPv6 addresses.
ipv6Regex := regexp.MustCompile(`([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}`)
// Find all matches in the input string.
matches := ipv6Regex.FindAllString(s, -1)
// Initialize an empty slice to hold the valid IPv6 addresses.
validIPv6Addresses := make([]string, 0)
// Iterate over the matches.
for _, match := range matches {
// Use net.ParseIP to validate the IP address.
// If it's a valid IPv6 address, it will be returned in IPv6 format.
// If it's not a valid IP address, or if it's an IPv4 address, nil will be returned.
if ip := net.ParseIP(match); ip != nil && strings.Contains(match, ":") {
validIPv6Addresses = append(validIPv6Addresses, match)
}
}
// Return the slice of valid IPv6 addresses.
return validIPv6Addresses
}
package tutter
import (
"bytes"
"encoding/json"
"fmt"
"github.com/samber/lo"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type Message struct {
Id int `json:"id"`
CreatedAt time.Time `json:"created_at"`
Text string `json:"text"`
Author struct {
Id int `json:"id"`
FirstSeen time.Time `json:"first_seen"`
Name string `json:"name"`
} `json:"author"`
Tags []interface{} `json:"tags"`
}
const url = "https://tutter.pproj.dev/api/poll"
var last = 0
func readLast() {
by, e := os.ReadFile("last")
if e != nil {
panic(e)
}
_last, e := strconv.Atoi(strings.TrimSpace(string(by)))
if e != nil {
panic(e)
}
last = _last
}
func init() {
readLast()
}
func LongPoll() <-chan Message {
ch := make(chan Message)
go func() {
for {
murl := fmt.Sprintf("%s?last=%d", url, last)
resp, e := http.Get(murl)
if e != nil {
log.Println(e)
continue
}
var messages []Message
js := json.NewDecoder(resp.Body)
e = js.Decode(&messages)
if e != nil {
log.Println(e)
continue
}
lo.ForEach(messages, func(msg Message, _ int) {
ch <- msg
})
ids := lo.Map(messages, func(item Message, _ int) int {
return item.Id
})
last = lo.Max(ids)
e = os.WriteFile("last", []byte(strconv.Itoa(last)), os.ModePerm)
if e != nil {
log.Println(e)
}
}
}()
return ch
}
type send struct {
Author string `json:"author"`
Text string `json:"text"`
}
var author = "ignoreme"
func init() {
name := os.Getenv("TUTTI_FRUTTI")
if name != "" {
author = name
}
}
func Send(msg string) error {
s := send{
Author: author,
Text: msg,
}
by, e := json.Marshal(s)
if e != nil {
return e
}
_, e = http.Post("https://tutter.pproj.dev/api/post", "application/json", bytes.NewReader(by))
return e
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment