Add PoC for listing files across a network with Gob

This commit is contained in:
raul 2024-06-17 07:51:37 +02:00
parent 9b33642a4e
commit 4b2777ea91
4 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,3 @@
module client
go 1.22.4

View File

@ -0,0 +1,43 @@
package main
import (
"encoding/gob"
"log"
"net"
"os"
)
type FileList struct {
List []Item
}
type Item struct {
Name string
IsFolder bool
}
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:1337")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
newList := FileList{}
list, _ := os.ReadDir("/home/raul")
for _, v := range list {
item := Item{
Name: v.Name(),
IsFolder: v.IsDir(),
}
newList.List = append(newList.List, item)
}
enc := gob.NewEncoder(conn)
err = enc.Encode(&newList)
if err != nil {
log.Fatalf("Error happened encoding: %v\n", err)
}
}

3
gob-listfiles/go.mod Normal file
View File

@ -0,0 +1,3 @@
module gob-file
go 1.22.4

47
gob-listfiles/main.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"encoding/gob"
"fmt"
"log"
"net"
)
type FileList struct {
List []Item
}
type Item struct {
Name string
IsFolder bool
}
func main() {
fmt.Println("Listening on port 1337")
ln, err := net.Listen("tcp", ":1337")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
}
go receiveFile(conn)
}
}
func receiveFile(conn net.Conn) {
dec := gob.NewDecoder(conn)
c := FileList{}
err := dec.Decode(&c)
if err != nil {
log.Println(err)
return
}
for _, v := range c.List {
fmt.Println(v.Name, v.IsFolder)
}
}