mirror of
https://github.com/asmogo/nws.git
synced 2024-12-12 18:36:22 +00:00
restructure protocol package (#48)
This commit is contained in:
parent
cf28dd5d9f
commit
ba1e856763
@ -276,7 +276,7 @@ func IsDomainName(name string) bool { //nolint:cyclop
|
||||
split := strings.Split(name, ".")
|
||||
|
||||
// Need a TLD and a domain.
|
||||
if len(split) < 2 { //nolint:gomnd
|
||||
if len(split) < 2 { //nolint:mnd
|
||||
return false
|
||||
}
|
||||
l := len(name)
|
||||
@ -325,8 +325,10 @@ func IsDomainName(name string) bool { //nolint:cyclop
|
||||
}
|
||||
|
||||
// RemoveScheme removes the scheme from a URL string.
|
||||
// If the URL string includes a scheme (e.g., "http://"), the scheme will be removed and the remaining string will be returned.
|
||||
// If the URL string includes a default scheme (e.g., "//"), the default scheme will be removed and the remaining string will be returned.
|
||||
// If the URL string includes a scheme (e.g., "http://"),
|
||||
// the scheme will be removed and the remaining string will be returned.
|
||||
// If the URL string includes a default scheme (e.g., "//"),
|
||||
// the default scheme will be removed and the remaining string will be returned.
|
||||
// If the URL string does not include a scheme, the original string will be returned unchanged.
|
||||
func RemoveScheme(s string) string {
|
||||
if strings.Contains(s, "://") {
|
||||
@ -374,7 +376,7 @@ func removeScheme(s string) string {
|
||||
|
||||
// index returns the starting index of the first occurrence of the specified scheme in the given string.
|
||||
// If the scheme is not found, it returns -1.
|
||||
// The returned index is incremented by the length of the scheme to obtain the starting position of the remaining string.
|
||||
// The returned int is incremented by the length of the scheme to obtain the starting position of the remaining string.
|
||||
func index(s, scheme string) int {
|
||||
return strings.Index(s, scheme) + len(scheme)
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package protocol
|
||||
package protocol_test
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/asmogo/nws/protocol"
|
||||
)
|
||||
|
||||
type args struct {
|
||||
@ -13,7 +15,7 @@ type args struct {
|
||||
type parseTest struct {
|
||||
name string
|
||||
args args
|
||||
want *URL
|
||||
want *protocol.URL
|
||||
wantErr bool
|
||||
}
|
||||
|
||||
@ -23,7 +25,7 @@ func TestParse(t *testing.T) {
|
||||
testCopy := test
|
||||
t.Run(testCopy.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := Parse(testCopy.args.s)
|
||||
got, err := protocol.Parse(testCopy.args.s)
|
||||
if (err != nil) != testCopy.wantErr {
|
||||
t.Errorf("Parse() error = %v, wantErr %v", err, testCopy.wantErr)
|
||||
return
|
||||
@ -37,9 +39,9 @@ func TestParse(t *testing.T) {
|
||||
|
||||
func createParseTests() []parseTest {
|
||||
return []parseTest{
|
||||
{name: "1", args: args{s: "http://D1Q78S3J78NIURJFEDQ74BJQCLH6AP35CKN66R3FELI0.9B7NTQSU4PBM2JJQJ0CMGHUENQON4GB28RLGQCH3D3NK2AQVFE70.nostr"}, want: &URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
|
||||
{name: "1", args: args{s: "http://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
|
||||
{name: "1", args: args{s: "https://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "https"}}, wantErr: false}, //nolint:lll
|
||||
{name: "1", args: args{s: "http://D1Q78S3J78NIURJFEDQ74BJQCLH6AP35CKN66R3FELI0.9B7NTQSU4PBM2JJQJ0CMGHUENQON4GB28RLGQCH3D3NK2AQVFE70.nostr"}, want: &protocol.URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
|
||||
{name: "1", args: args{s: "http://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &protocol.URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "http"}}, wantErr: false}, //nolint:lll
|
||||
{name: "1", args: args{s: "https://d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr"}, want: &protocol.URL{IsDomain: true, TLD: "nostr", Name: "9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70", SubName: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0", URL: &url.URL{Host: "d1q78s3j78niurjfedq74bjqclh6ap35ckn66r3feli0.9b7ntqsu4pbm2jjqj0cmghuenqon4gb28rlgqch3d3nk2aqvfe70.nostr", Scheme: "https"}}, wantErr: false}, //nolint:lll
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ package protocol
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type MessageType string
|
||||
@ -16,11 +16,11 @@ const (
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Key uuid.UUID `json:"key,omitempty"`
|
||||
Type MessageType `json:"type,omitempty"`
|
||||
Data []byte `json:"data,omitempty"`
|
||||
Destination string `json:"destination,omitempty"`
|
||||
EntryPublicAddress string `json:"entryPublicAddress,omitempty"`
|
||||
Key uuid.UUID `json:"key,omitempty"` // unique identifier for the message
|
||||
Type MessageType `json:"type,omitempty"` // type of message
|
||||
Data []byte `json:"data,omitempty"` // data to be sent
|
||||
Destination string `json:"destination,omitempty"` // destination to send the message
|
||||
EntryPublicAddress string `json:"entryPublicAddress,omitempty"` // public ip address of the entry node (used for reverse connect)
|
||||
}
|
||||
|
||||
type MessageOption func(*Message)
|
||||
|
@ -2,16 +2,21 @@ package protocol
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
padding = "02"
|
||||
)
|
||||
|
||||
func GetEncryptionKeys(privateKey, publicKey string) ([]byte, []byte, error) {
|
||||
targetPublicKeyBytes, err := hex.DecodeString("02" + publicKey)
|
||||
targetPublicKeyBytes, err := hex.DecodeString(padding + publicKey)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, fmt.Errorf("failed to decode public key: %w", err)
|
||||
}
|
||||
privateKeyBytes, err := hex.DecodeString(privateKey)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, fmt.Errorf("failed to decode private key: %w", err)
|
||||
}
|
||||
return privateKeyBytes, targetPublicKeyBytes, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user