Skip to content

Commit 64624b0

Browse files
committed
Library implementation.
1 parent 2f8bd41 commit 64624b0

15 files changed

+858
-0
lines changed

announce_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import (
21+
"context"
22+
"crypto/ed25519"
23+
"crypto/rand"
24+
"encoding/base64"
25+
"testing"
26+
)
27+
28+
func TestAnnounce(t *testing.T) {
29+
pk, sk, err := ed25519.GenerateKey(rand.Reader)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
vals := []string{
35+
"p7qaewjgnvnaeihhyybmoofd5avh665kr3awoxlh5rt6ox743kjdr6qd.onion:666",
36+
base64.StdEncoding.EncodeToString(pk),
37+
"12345:54321,666:3521",
38+
}
39+
40+
ret, err := ann.Init(ann{}, context.Background(), vals)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
for _, i := range ret {
45+
if _, err := base64.StdEncoding.DecodeString(i); err != nil {
46+
t.Fatal(err)
47+
}
48+
}
49+
50+
vals = []string{
51+
"p7qaewjgnvnaeihhyybmoofd5avh665kr3awoxlh5rt6ox743kjdr6qd.onion:666",
52+
base64.StdEncoding.EncodeToString(ed25519.Sign(sk, []byte(ret[0]))),
53+
}
54+
55+
ret, err = ann.Validate(ann{}, context.Background(), vals)
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
for _, i := range ret {
60+
if err := validateOnionInternal(i); err != nil {
61+
t.Fatal(err)
62+
}
63+
}
64+
}

config.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import (
21+
"crypto/ed25519"
22+
"net"
23+
)
24+
25+
// Config is the configuration structure, to be filled by library user.
26+
type Config struct {
27+
Listen *net.TCPAddr // Local listen address for the JSON-RPC server
28+
TorAddr *net.TCPAddr // Tor SOCKS5 proxy address, filled by SpawnTor()
29+
Datadir string // Path to data directory
30+
Portmap []string // The peer's portmap, to be mapped in the Tor HS
31+
Seeds []string // Initial peer(s)
32+
Announce bool // Announce or not
33+
}
34+
35+
// SignKey is an ed25519 private key, to be assigned by library user.
36+
var SignKey ed25519.PrivateKey
37+
38+
// Onion is the library user's something.onion:port identifier. It can be read
39+
// from the datadir once Tor is spawned.
40+
var Onion string
41+
42+
// Cfg is the global config structure, to be filled by library user.
43+
var Cfg = Config{}
44+
45+
// Peers is the global map of peers
46+
var Peers = map[string]Peer{}

cryptohelpers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import (
21+
"crypto/rand"
22+
"encoding/base64"
23+
"fmt"
24+
)
25+
26+
// RandomGarbage returns a base64 encoded string of n bytes.
27+
func RandomGarbage(n int) (string, error) {
28+
garbage := make([]byte, n)
29+
read, err := rand.Read(garbage)
30+
if err != nil {
31+
return "", err
32+
}
33+
if n != read {
34+
return "", fmt.Errorf("read %d, but requested %d bytes", read, n)
35+
}
36+
return base64.StdEncoding.EncodeToString(garbage), nil
37+
}

cryptohelpers_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import (
21+
"testing"
22+
)
23+
24+
func TestRandomGarbage(t *testing.T) {
25+
if _, err := RandomGarbage(128); err != nil {
26+
t.Fatal(err)
27+
}
28+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/parazyd/tordam
2+
3+
go 1.16

logging.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import (
21+
"log"
22+
"strings"
23+
)
24+
25+
func rpcWarn(msg ...string) {
26+
text := strings.Join(msg[1:], " ")
27+
log.Printf("RPC warning: (%s) %s", msg[0], text)
28+
}
29+
func rpcInfo(msg ...string) {
30+
text := strings.Join(msg[1:], " ")
31+
log.Printf("RPC info: (%s) %s", msg[0], text)
32+
}
33+
func rpcInternalErr(msg ...string) {
34+
text := strings.Join(msg[1:], " ")
35+
log.Printf("RPC internal error: (%s) %s", msg[0], text)
36+
}

net.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import "net"
21+
22+
// GetAvailableListener is a helper function to return a *net.TCPAddr on some
23+
// port that is available for listening on the system. It uses the :0 port
24+
// which the kernel utilizes to return a random available port.
25+
func GetAvailableListener() (*net.TCPAddr, error) {
26+
addr, err := net.ResolveTCPAddr("tcp4", "localhost:0")
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
l, err := net.ListenTCP("tcp4", addr)
32+
if err != nil {
33+
return nil, err
34+
}
35+
defer l.Close()
36+
return l.Addr().(*net.TCPAddr), nil
37+
}

net_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import "testing"
21+
22+
// GetAvailableListener is a helper function to return a *net.TCPAddr on some
23+
// port that is available for listening on the system. It uses the :0 port
24+
// which the kernel utilizes to return a random available port.
25+
func TestGetAvailableListener(t *testing.T) {
26+
if _, err := GetAvailableListener(); err != nil {
27+
t.Fatal(err)
28+
}
29+
}

peer.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2017-2021 Ivan Jelincic <[email protected]>
2+
//
3+
// This file is part of tordam
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package tordam
19+
20+
import (
21+
"crypto/ed25519"
22+
)
23+
24+
// Peer is the base struct for any peer in the network.
25+
type Peer struct {
26+
Pubkey ed25519.PublicKey
27+
Portmap []string
28+
Nonce string
29+
SelfRevoke string // Our revoke key we use to update our data
30+
PeerRevoke string // Peer's revoke key if they wish to update their data
31+
LastSeen int64
32+
Trusted int // Trusted is int because of possible levels of trust
33+
}

0 commit comments

Comments
 (0)