Skip to content

Commit

Permalink
Merge pull request #112 from Akilan1999/cname-entry
Browse files Browse the repository at this point in the history
Added possibility to add domain name for nodes on P2PRC
  • Loading branch information
Akilan1999 authored Oct 19, 2024
2 parents 9d9e385 + 5a5985a commit 64f8d77
Show file tree
Hide file tree
Showing 18 changed files with 588 additions and 336 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ test*
# Ignore public and private keys
p2prc.publicKey
p2prc.privateKey

# Ignore pem files
*.pem
4 changes: 2 additions & 2 deletions Bindings/Client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func EscapeFirewall(HostOutsideNATIP string, HostOutsideNATPort string, internal
}

//export MapPort
func MapPort(Port string) *C.char {
entireAddress, _, err := abstractions.MapPort(Port)
func MapPort(Port string, DomainName string) *C.char {
entireAddress, _, err := abstractions.MapPort(Port, DomainName)
if err != nil {
return C.CString(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions abstractions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func Start() (*gin.Engine, error) {
}

// MapPort Creates a reverse proxy connection and maps the appropriate port
func MapPort(port string) (entireAddres string, mapPort string, err error) {
entireAddres, mapPort, err = server.MapPort(port)
func MapPort(port string, domainName string) (entireAddres string, mapPort string, err error) {
entireAddres, mapPort, err = server.MapPort(port, domainName)
return
}

Expand Down
4 changes: 2 additions & 2 deletions client/MAPPort.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"net/http"
)

func MAPPort(port string) (string, error) {
func MAPPort(port string, domainName string) (string, error) {
Config, err := config.ConfigInit(nil, nil)
if err != nil {
return "", err
}

//if version == "version 6" {
URL := "http://0.0.0.0:" + Config.ServerPort + "/MAPPort?port=" + port
URL := "http://0.0.0.0:" + Config.ServerPort + "/MAPPort?port=" + port + "&domain_name=" + domainName
//} else {
// URL = "http://" + IP + ":" + serverPort + "/server_info"
//}
Expand Down
2 changes: 1 addition & 1 deletion cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ var CliAction = func(ctx *cli.Context) error {
}

if MAPPort != "" {
address, err := client.MAPPort(MAPPort)
address, err := client.MAPPort(MAPPort, DomainName)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
RemoveContainerGroup bool
RemoveGroup string
MAPPort string
DomainName string
//FRPProxy bool
// Generate only allowed in dev release
// -- REMOVE ON REGULAR RELEASE --
Expand Down Expand Up @@ -212,6 +213,13 @@ var AppConfigFlags = []cli.Flag{
EnvVars: []string{"MAPPORT"},
Destination: &MAPPort,
},
&cli.StringFlag{
Name: "DomainName",
Aliases: []string{"dn"},
Usage: "While mapping ports allows to set a domain name to create a mapping in the proxy server",
EnvVars: []string{"DOMAINNAME"},
Destination: &DomainName,
},
// Generate only allowed in dev release
// -- REMOVE ON REGULAR RELEASE --
&cli.StringFlag{
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ type Config struct {
PluginPath string
TrackContainersPath string
ServerPort string
ProxyPort string
GroupTrackContainersPath string
FRPServerPort string
BehindNAT string
IPTableKey string
PublicKeyFile string
PrivateKeyFile string
PemFile string
KeyFile string
BareMetal string
CustomConfig interface{}
//NetworkInterface string
Expand Down
10 changes: 10 additions & 0 deletions config/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No
Defaults.TrackContainersPath = defaultPath + "client/trackcontainers/trackcontainers.json"
Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json"
Defaults.ServerPort = "8088"
Defaults.ProxyPort = ""
Defaults.FRPServerPort = "True"
Defaults.CustomConfig = CustomConfig
Defaults.BehindNAT = "True"
Expand All @@ -94,6 +95,15 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No
Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey"
Defaults.BareMetal = "False"

// Generate certificate files for SSL
err = GenerateCertificate()
if err != nil {
return nil, err
}

Defaults.PemFile = defaultPath + "cert.pem"
Defaults.KeyFile = defaultPath + "key.pem"

PrivateKeyExists, err := FileExists(Defaults.PrivateKeyFile)
if err != nil {
return nil, err
Expand Down
177 changes: 177 additions & 0 deletions config/generate/generateCertificate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.x

// Generate a self-signed X.509 certificate for a TLS server. Outputs to
// 'cert.pem' and 'key.pem' and will overwrite existing files.

package generate

import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"github.com/Akilan1999/p2p-rendering-computation/p2p"
"log"
"math/big"
"net"
"os"
"strings"
"time"
)

var (
host = ""
validFrom = ""
validFor = 365 * 24 * time.Hour
isCA = false
rsaBits = 2048
ecdsaCurve = ""
ed25519Key = false
)

func publicKey(priv any) any {
switch k := priv.(type) {
case *rsa.PrivateKey:
return &k.PublicKey
case *ecdsa.PrivateKey:
return &k.PublicKey
case ed25519.PrivateKey:
return k.Public().(ed25519.PublicKey)
default:
return nil
}
}

func GenerateCertificate() error {
var priv any
var err error

//flag.Parse()

host, err = p2p.CurrentPublicIP()

if err != nil {
return err
}

if len(host) == 0 {
log.Fatalf("Missing required --host parameter")
}
switch ecdsaCurve {
case "":
if ed25519Key {
_, priv, err = ed25519.GenerateKey(rand.Reader)
} else {
priv, err = rsa.GenerateKey(rand.Reader, rsaBits)
}
case "P224":
priv, err = ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
case "P256":
priv, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
case "P384":
priv, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
case "P521":
priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
default:
log.Fatalf("Unrecognized elliptic curve: %q", ecdsaCurve)
}
if err != nil {
log.Fatalf("Failed to generate private key: %v", err)
}

// ECDSA, ED25519 and RSA subject keys should have the DigitalSignature
// KeyUsage bits set in the x509.Certificate template
keyUsage := x509.KeyUsageDigitalSignature
// Only RSA subject keys should have the KeyEncipherment KeyUsage bits set. In
// the context of TLS this KeyUsage is particular to RSA key exchange and
// authentication.
if _, isRSA := priv.(*rsa.PrivateKey); isRSA {
keyUsage |= x509.KeyUsageKeyEncipherment
}

var notBefore time.Time
if len(validFrom) == 0 {
notBefore = time.Now()
} else {
notBefore, err = time.Parse("Jan 2 15:04:05 2006", validFrom)
if err != nil {
log.Fatalf("Failed to parse creation date: %v", err)
}
}

notAfter := notBefore.Add(validFor)

serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Fatalf("Failed to generate serial number: %v", err)
}

template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Acme Co"},
},
NotBefore: notBefore,
NotAfter: notAfter,

KeyUsage: keyUsage,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}

hosts := strings.Split(host, ",")
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}

if isCA {
template.IsCA = true
template.KeyUsage |= x509.KeyUsageCertSign
}

derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
if err != nil {
log.Fatalf("Failed to create certificate: %v", err)
}

certOut, err := os.Create("cert.pem")
if err != nil {
log.Fatalf("Failed to open cert.pem for writing: %v", err)
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
log.Fatalf("Failed to write data to cert.pem: %v", err)
}
if err := certOut.Close(); err != nil {
log.Fatalf("Error closing cert.pem: %v", err)
}
log.Print("wrote cert.pem\n")

keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Failed to open key.pem for writing: %v", err)
}
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
log.Fatalf("Unable to marshal private key: %v", err)
}
if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
log.Fatalf("Failed to write data to key.pem: %v", err)
}
if err := keyOut.Close(); err != nil {
log.Fatalf("Error closing key.pem: %v", err)
}
log.Print("wrote key.pem\n")

return nil
}
1 change: 1 addition & 0 deletions config/generate/generateFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func GenerateIPTableFile(rootNodes []p2p.IpAddress) (err error) {
rootnode.ServerPort = "8078"
rootnode.NAT = "False"
rootnode.Ipv4 = "217.76.63.222"
rootnode.ProxyServer = "True"

rootnodes.IpAddress = append(rootnodes.IpAddress, rootnode)
} else {
Expand Down
Loading

0 comments on commit 64f8d77

Please sign in to comment.