Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint #42

Merged
merged 1 commit into from
Sep 1, 2024
Merged

lint #42

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions cmd/nws/nws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log/slog"

"github.com/asmogo/nws/config"
Expand Down Expand Up @@ -34,18 +35,18 @@ func main() {
func updateConfigFlag(cmd *cobra.Command, cfg *config.ExitConfig) error {
httpsPort, err := cmd.Flags().GetInt32("port")
if err != nil {
return err
return fmt.Errorf("failed to get https port: %w", err)
}
httpTarget, err := cmd.Flags().GetString("target")
if err != nil {
return err
return fmt.Errorf("failed to get http target: %w", err)
}
cfg.HttpsPort = httpsPort
cfg.HttpsTarget = httpTarget
return nil
}

func startExitNode(cmd *cobra.Command, args []string) {
func startExitNode(cmd *cobra.Command, _ []string) {
slog.Info("Starting exit node")
// load the configuration
cfg, err := config.LoadConfig[config.ExitConfig]()
Expand All @@ -65,17 +66,14 @@ func startExitNode(cmd *cobra.Command, args []string) {
exitNode.ListenAndServe(ctx)
}

func startEntryNode(cmd *cobra.Command, args []string) {
func startEntryNode(cmd *cobra.Command, _ []string) {
slog.Info("Starting entry node")
cfg, err := config.LoadConfig[config.EntryConfig]()
if err != nil {
panic(err)
}

// create a new gw server
// and start it
socksProxy := proxy.New(cmd.Context(), cfg)

err = socksProxy.Start()
if err != nil {
panic(err)
Expand Down
14 changes: 10 additions & 4 deletions protocol/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package protocol
import (
"encoding/json"
"github.com/google/uuid"

"fmt"
)

type MessageType string

var (
MessageTypeSocks5 = MessageType("SOCKS5")
const (
MessageTypeSocks5 = MessageType("SOCKS5RESPONSE")
MessageConnect = MessageType("CONNECT")
MessageConnectReverse = MessageType("CONNECTR")
)
Expand Down Expand Up @@ -60,13 +62,17 @@ func NewMessage(configs ...MessageOption) *Message {
return m
}
func MarshalJSON(m *Message) ([]byte, error) {
return json.Marshal(m)
data, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("could not marshal message: %w", err)
}
return data, nil
}

func UnmarshalJSON(data []byte) (*Message, error) {
m := NewMessage()
if err := json.Unmarshal(data, &m); err != nil {
return nil, err
return nil, fmt.Errorf("could not unmarshal message: %w", err)
}
return m, nil
}
11 changes: 4 additions & 7 deletions protocol/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package protocol

import (
"fmt"
"log/slog"

"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip04"
)
Expand All @@ -22,17 +20,16 @@ const KindPrivateKeyEvent int = 38335

// EventSigner represents a signer that can create and sign events.
//
// EventSigner provides methods for creating unsigned events, creating signed events
// EventSigner provides methods for creating unsigned events, creating signed events.
type EventSigner struct {
PublicKey string
privateKey string
}

// NewEventSigner creates a new EventSigner
// NewEventSigner creates a new EventSigner.
func NewEventSigner(privateKey string) (*EventSigner, error) {
myPublicKey, err := nostr.GetPublicKey(privateKey)
if err != nil {
slog.Error("could not generate pubkey")
return nil, fmt.Errorf("could not generate public key: %w", err)
}
signer := &EventSigner{
Expand All @@ -43,7 +40,7 @@ func NewEventSigner(privateKey string) (*EventSigner, error) {
}

// CreateEvent creates a new Event with the provided tags. The Public Key and the
// current timestamp are set automatically. The Kind is set to KindEphemeralEvent
// current timestamp are set automatically. The Kind is set to KindEphemeralEvent.
func (s *EventSigner) CreateEvent(kind int, tags nostr.Tags) nostr.Event {
return nostr.Event{
PubKey: s.PublicKey,
Expand All @@ -60,7 +57,7 @@ func (s *EventSigner) CreateEvent(kind int, tags nostr.Tags) nostr.Event {
// The method then calls CreateEvent to create a new unsigned event with the provided tags.
// The encrypted message is set as the content of the event.
// Finally, the event is signed with the private key of the EventSigner, setting the event ID and event Sig fields.
// The signed event is returned along with any error that occurs
// The signed event is returned along with any error that occurs.
func (s *EventSigner) CreateSignedEvent(
targetPublicKey string,
kind int,
Expand Down
34 changes: 8 additions & 26 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,35 @@ package proxy

import (
"context"
"fmt"
"net"

"github.com/asmogo/nws/config"
"github.com/asmogo/nws/netstr"
"github.com/asmogo/nws/socks5"
"github.com/nbd-wtf/go-nostr"
"net"
)

type Proxy struct {
config *config.EntryConfig // the configuration for the gateway
// a list of nostr relays to publish events to
relays []*nostr.Relay // deprecated -- should be used for default relay configuration
pool *nostr.SimplePool
socksServer *socks5.Server
}

func New(ctx context.Context, config *config.EntryConfig) *Proxy {
s := &Proxy{
proxy := &Proxy{
config: config,
pool: nostr.NewSimplePool(ctx),
}
socksServer, err := socks5.New(&socks5.Config{
AuthMethods: nil,
Credentials: nil,
Resolver: netstr.NewNostrDNS(s.pool, config.NostrRelays),
Rules: nil,
Rewriter: nil,
BindIP: net.IP{0, 0, 0, 0},
Logger: nil,
Dial: nil,
}, s.pool, config)
Resolver: netstr.NewNostrDNS(proxy.pool, config.NostrRelays),
BindIP: net.IP{0, 0, 0, 0},
}, proxy.pool, config)
if err != nil {
panic(err)
}
s.socksServer = socksServer
// publish the event to two relays
for _, relayUrl := range config.NostrRelays {

relay, err := s.pool.EnsureRelay(relayUrl)
if err != nil {
fmt.Println(err)
continue
}
s.relays = append(s.relays, relay)
fmt.Printf("added relay connection to %s\n", relayUrl)
}
return s
proxy.socksServer = socksServer
return proxy
}

// Start should start the server
Expand Down
17 changes: 12 additions & 5 deletions socks5/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,10 @@ func SendReply(w io.Writer, resp uint8, addr *AddrSpec) error {

// Send the message
_, err := w.Write(msg)
return err
if err != nil {
return fmt.Errorf("failed to send reply: %w", err)
}
return nil
}

type closeWriter interface {
Expand All @@ -385,16 +388,20 @@ type closeWriter interface {
// down a dedicated channel
func Proxy(dst io.Writer, src io.Reader, errCh chan error) {
_, err := io.Copy(dst, src)

checkError(errCh, err)
if tcpConn, ok := dst.(closeWriter); ok {
tcpConn.CloseWrite()
err = tcpConn.CloseWrite()
}
if conn, ok := dst.(io.Closer); ok {
conn.Close()
err = conn.Close()
}
if conn, ok := src.(io.Closer); ok {
conn.Close()
err = conn.Close()
}
checkError(errCh, err)
}

func checkError(errCh chan error, err error) {
if errCh != nil {
errCh <- err
}
Expand Down
Loading