@ -1,2 +1,8 @@ |
|||
# blockchainIDsystem |
|||
A blockchain based anonymous login system |
|||
|
|||
|
|||
- p2p |
|||
- blockchain |
|||
- rsa |
|||
- blind signature |
@ -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" |
|||
} |
|||
``` |
@ -0,0 +1,6 @@ |
|||
{ |
|||
"ip": "127.0.0.1", |
|||
"port": "3001", |
|||
"serverip": "127.0.0.1", |
|||
"serverport": "3000" |
|||
} |
@ -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() |
|||
} |
@ -0,0 +1,9 @@ |
|||
package main |
|||
|
|||
import "log" |
|||
|
|||
func check(err error) { |
|||
if err != nil { |
|||
log.Println(err) |
|||
} |
|||
} |
@ -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) |
|||
} |
@ -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) |
|||
} |
|||
} |
@ -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) |
|||
} |
@ -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 |
|||
} |