diff --git a/README.md b/README.md index 05d471d..93bb762 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # blockchainIDsystem A blockchain based anonymous login system + + +- p2p +- blockchain +- rsa +- blind signature diff --git a/peer/README.md b/peer/README.md new file mode 100644 index 0000000..9f577ca --- /dev/null +++ b/peer/README.md @@ -0,0 +1,21 @@ +# Peer + +To run as a normal peer: +``` +./peer +``` + +To run as a p2p server: +``` +./peer server +``` + +Needs the config.json file: +```json +{ + "ip": "127.0.0.1", + "port": "3001", + "serverip": "127.0.0.1", + "serverport": "3000" +} +``` diff --git a/peer/config.json b/peer/config.json new file mode 100755 index 0000000..28771f9 --- /dev/null +++ b/peer/config.json @@ -0,0 +1,6 @@ +{ + "ip": "127.0.0.1", + "port": "3001", + "serverip": "127.0.0.1", + "serverport": "3000" +} diff --git a/peer/connections.go b/peer/connections.go new file mode 100644 index 0000000..c961421 --- /dev/null +++ b/peer/connections.go @@ -0,0 +1,71 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net" + + "github.com/fatih/color" +) + +func acceptPeers(peer Peer) { + fmt.Println("accepting peers at: " + peer.Port) + l, err := net.Listen("tcp", peer.IP+":"+peer.Port) + if err != nil { + log.Println("Error accepting peers. Listening port: " + peer.Port) + running = false + } + for running { + conn, err := l.Accept() + if err != nil { + log.Println("Error accepting peers. Error accepting connection") + running = false + } + var newPeer Peer + newPeer.IP = getIPFromConn(conn) + newPeer.Port = getPortFromConn(conn) + newPeer.Conn = conn + listPeers = append(listPeers, newPeer) + go handleConn(conn) + } +} +func connectToPeer(peer Peer) { + conn, err := net.Dial("tcp", peer.IP+":"+peer.Port) + if err != nil { + log.Println("Error connecting to: " + peer.IP + ":" + peer.Port) + return + } + peer.Conn = conn + listPeers = append(listPeers, peer) + go handleConn(conn) +} +func handleConn(conn net.Conn) { + connRunning := true + log.Println("handling conn: " + conn.RemoteAddr().String()) + //reply to the conn + msgB := newMsgBytes("Hi", "New Peer connected") + _, err := conn.Write(msgB) + if err != nil { + check(err) + } + + buffer := make([]byte, 1024) + for connRunning { + bytesRead, err := conn.Read(buffer) + if err != nil { + log.Println(err) + connRunning = false + } else { + s := string(buffer[0:bytesRead]) + var msg Msg + err := json.Unmarshal([]byte(s), &msg) + check(err) + log.Println("[New msg] [Title]: " + msg.Type + " [Content]: " + msg.Content) + color.Green(msg.Content) + } + } + //TODO add that if the peer closed is the p2p server, show a warning message at the peer + log.Println("Peer: " + conn.RemoteAddr().String() + " connection closed") + conn.Close() +} diff --git a/peer/errors.go b/peer/errors.go new file mode 100755 index 0000000..e997ec1 --- /dev/null +++ b/peer/errors.go @@ -0,0 +1,9 @@ +package main + +import "log" + +func check(err error) { + if err != nil { + log.Println(err) + } +} diff --git a/peer/log.go b/peer/log.go new file mode 100755 index 0000000..e8f391a --- /dev/null +++ b/peer/log.go @@ -0,0 +1,24 @@ +package main + +import ( + "io" + "log" + "os" + "strings" + "time" +) + +func savelog() { + timeS := time.Now().String() + _ = os.Mkdir("logs", os.ModePerm) + //next 3 lines are to avoid windows filesystem errors + timeS = strings.Replace(timeS, " ", "_", -1) + timeS = strings.Replace(timeS, ".", "-", -1) + timeS = strings.Replace(timeS, ":", "-", -1) + logFile, err := os.OpenFile("logs/log-"+timeS+".log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) + if err != nil { + panic(err) + } + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) +} diff --git a/peer/main.go b/peer/main.go new file mode 100644 index 0000000..b5d5ba7 --- /dev/null +++ b/peer/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "net" + "os" + "time" + + "github.com/fatih/color" +) + +type Peer struct { + IP string + Port string + Role string //client or server + Conn net.Conn +} +type Msg struct { + Type string `json:"type"` + Content string `json:"content"` +} + +var listPeers []Peer +var running bool + +func main() { + //initialize some vars + running = true + var peer Peer + + color.Blue("Starting Peer") + readConfig("config.json") + + peer.IP = config.IP + peer.Port = config.Port + peer.Role = "client" + + //read flags, to know if is runned as p2p server + if len(os.Args) > 1 { + if os.Args[1] == "server" { + color.Yellow("Running as p2p server") + peer.Role = "server" + peer.Port = config.ServerPort + go acceptPeers(peer) + } + } + fmt.Println(peer) + if peer.Role == "client" { + var newPeer Peer + newPeer.IP = config.ServerIP + newPeer.Port = config.ServerPort + newPeer.Role = "server" + connectToPeer(newPeer) + } + + for running { + time.Sleep(1000 * time.Millisecond) + } +} diff --git a/peer/peer b/peer/peer new file mode 100755 index 0000000..a5d02ee Binary files /dev/null and b/peer/peer differ diff --git a/peer/readConfig.go b/peer/readConfig.go new file mode 100755 index 0000000..0fdaf99 --- /dev/null +++ b/peer/readConfig.go @@ -0,0 +1,23 @@ +package main + +import ( + "encoding/json" + "io/ioutil" +) + +//Config reads the config +type Config struct { + IP string `json:"ip"` + Port string `json:"port"` + ServerIP string `json:"serverip"` + ServerPort string `json:"serverport"` +} + +var config Config + +func readConfig(path string) { + file, err := ioutil.ReadFile(path) + check(err) + content := string(file) + json.Unmarshal([]byte(content), &config) +} diff --git a/peer/utils.go b/peer/utils.go new file mode 100644 index 0000000..be7df16 --- /dev/null +++ b/peer/utils.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "net" + "strings" +) + +func newMsgBytes(msgtype string, msgcontent string) []byte { + var msg Msg + msg.Type = msgtype + msg.Content = msgcontent + msgS, err := json.Marshal(msg) + check(err) + return msgS +} +func getIPFromConn(conn net.Conn) string { + s := conn.RemoteAddr().String() + s = strings.Split(s, ":")[0] + s = strings.Trim(s, ":") + return s +} +func getPortFromConn(conn net.Conn) string { + s := conn.RemoteAddr().String() + s = strings.Split(s, ":")[1] + s = strings.Trim(s, ":") + return s +}