|
| 1 | +package net |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/binary" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "github.com/gorilla/websocket" |
| 9 | + "github.com/mr-tron/base58/base58" |
| 10 | + "github.com/multiformats/go-multihash" |
| 11 | +) |
| 12 | + |
| 13 | +type WebRelayManager struct { |
| 14 | + webrelays []string |
| 15 | + peerID string |
| 16 | +} |
| 17 | + |
| 18 | +type EncryptedMessage struct { |
| 19 | + Message string `json:"encryptedMessage"` |
| 20 | + Recipient string `json:"recipient"` |
| 21 | +} |
| 22 | + |
| 23 | +type TypedMessage struct{ |
| 24 | + Type string |
| 25 | + Data json.RawMessage |
| 26 | +} |
| 27 | + |
| 28 | +type SubscribeMessage struct { |
| 29 | + UserID string `json:"userID"` |
| 30 | + SubscriptionKey string `json:"subscriptionKey"` |
| 31 | +} |
| 32 | + |
| 33 | +func NewWebRelayManager(webrelays []string, sender string) *WebRelayManager { |
| 34 | + return &WebRelayManager{webrelays, sender} |
| 35 | +} |
| 36 | + |
| 37 | +func (wrm *WebRelayManager) SendRelayMessage(ciphertext string, recipient string) { |
| 38 | + encryptedmessage := EncryptedMessage{ |
| 39 | + Message: ciphertext, |
| 40 | + Recipient: recipient, |
| 41 | + } |
| 42 | + |
| 43 | + data, _ := json.Marshal(encryptedmessage) |
| 44 | + |
| 45 | + typedmessage := TypedMessage{ |
| 46 | + Type: "EncryptedMessage", |
| 47 | + Data: data, |
| 48 | + } |
| 49 | + |
| 50 | + outgoing, _ := json.Marshal(typedmessage) |
| 51 | + fmt.Println(string(outgoing)) |
| 52 | + |
| 53 | + // Transmit the encrypted message to the webrelay |
| 54 | + wrm.authToWebRelay(wrm.webrelays[0], outgoing) |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +func (wrm *WebRelayManager) authToWebRelay(server string, msg []byte) { |
| 59 | + |
| 60 | + // Generate subscription key for web relay |
| 61 | + peerIDMultihash, _ := multihash.FromB58String(wrm.peerID) |
| 62 | + decoded, _ := multihash.Decode(peerIDMultihash) |
| 63 | + digest := decoded.Digest |
| 64 | + prefix := digest[:8] |
| 65 | + |
| 66 | + prefix64 := binary.BigEndian.Uint64(prefix) |
| 67 | + |
| 68 | + // Then shifting |
| 69 | + shiftedPrefix64 := prefix64>>uint(48) |
| 70 | + |
| 71 | + // Then converting back to a byte array |
| 72 | + shiftedBytes := make([]byte, 8) |
| 73 | + binary.BigEndian.PutUint64(shiftedBytes, shiftedPrefix64) |
| 74 | + |
| 75 | + hashedShiftedPrefix := sha256.Sum256(shiftedBytes) |
| 76 | + |
| 77 | + subscriptionKey, _ := multihash.Encode(hashedShiftedPrefix[:], multihash.SHA2_256) |
| 78 | + |
| 79 | + // Generate subscribe message |
| 80 | + subscribeMessage := SubscribeMessage{ |
| 81 | + UserID: wrm.peerID, |
| 82 | + SubscriptionKey: base58.Encode([]byte(subscriptionKey)), |
| 83 | + } |
| 84 | + |
| 85 | + data, _ := json.Marshal(subscribeMessage) |
| 86 | + typedmessage := TypedMessage{ |
| 87 | + Type: "SubscribeMessage", |
| 88 | + Data: data, |
| 89 | + } |
| 90 | + fmt.Println(typedmessage) |
| 91 | + |
| 92 | + socketmessage, _ := json.Marshal(typedmessage) |
| 93 | + |
| 94 | + // Connect to websocket server |
| 95 | + fmt.Printf("connecting to %s", server) |
| 96 | + |
| 97 | + c, _, err := websocket.DefaultDialer.Dial(server, nil) |
| 98 | + if err != nil { |
| 99 | + log.Fatal("dial:", err) |
| 100 | + } |
| 101 | + defer c.Close() |
| 102 | + |
| 103 | + err = c.WriteMessage(websocket.TextMessage, socketmessage) |
| 104 | + if err != nil { |
| 105 | + fmt.Println("write:", err) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + err = c.WriteMessage(websocket.TextMessage, msg) |
| 110 | + if err != nil { |
| 111 | + fmt.Println("write:", err) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | +} |
| 116 | + |
0 commit comments