Skip to content

Commit

Permalink
Clear proxy cache/state on stop
Browse files Browse the repository at this point in the history
  • Loading branch information
buffrr committed Jul 30, 2021
1 parent ba9c421 commit 9cecd51
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
11 changes: 1 addition & 10 deletions internal/resolvers/proc/hns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proc
import (
"bufio"
"fmt"
"github.com/buffrr/letsdane/resolver"
"io"
"log"
"os/exec"
Expand All @@ -20,7 +19,6 @@ type HNSProc struct {
rootAddr string
cmd *exec.Cmd
Verbose bool
Client *resolver.Stub
procStarted bool
height uint64
lastHeightUpdate time.Time
Expand All @@ -34,11 +32,6 @@ func NewHNSProc(procPath string, rootAddr, recursiveAddr string, opts ...string)
args := []string{"-n", rootAddr, "-r", recursiveAddr}
args = append(args, opts...)

rs, err := resolver.NewStub(recursiveAddr)
if err != nil {
return nil, err
}

if !strings.HasSuffix(procPath, processExtension) {
procPath += processExtension
}
Expand All @@ -48,9 +41,7 @@ func NewHNSProc(procPath string, rootAddr, recursiveAddr string, opts ...string)
args: args,
resolverAddr: recursiveAddr,
rootAddr: rootAddr,

Client: rs,
Verbose: true,
Verbose: true,
}

return p, nil
Expand Down
60 changes: 41 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fingertip/internal/ui"
"fmt"
"github.com/buffrr/letsdane"
"github.com/buffrr/letsdane/resolver"
"github.com/emersion/go-autostart"
"github.com/pkg/browser"
"log"
Expand All @@ -21,7 +22,6 @@ import (

type App struct {
proc *proc.HNSProc
hip5 *resolvers.HIP5Resolver
server *http.Server
config *config.App
usrConfig *config.User
Expand Down Expand Up @@ -251,43 +251,65 @@ func NewApp(appConfig *config.App) (*App, error) {

app.proc = hnsProc

app.hip5 = resolvers.NewHIP5Resolver(app.proc.Client, usrConfig.RootAddr, hnsProc.Synced)
eth, err := resolvers.NewEthereum(usrConfig.EthereumEndpoint)
app.server, err = app.newProxyServer()
if err != nil {
return nil, err
}

app.hip5.RegisterHandler("_eth", eth.Handler)
appConfig.Proxy.Resolver = app.hip5
return app, nil
}

app.server, err = newProxy(appConfig, usrConfig.ProxyAddr)
func (a *App) NewResolver() (resolver.Resolver, error) {
rs, err := resolver.NewStub(a.usrConfig.RecursiveAddr)
if err != nil {
return nil, err
}

return app, nil
hip5 := resolvers.NewHIP5Resolver(rs, a.usrConfig.RootAddr, a.proc.Synced)
ethExt, err := resolvers.NewEthereum(a.usrConfig.EthereumEndpoint)
if err != nil {
return nil, err
}

// Register HIP-5 handlers
hip5.RegisterHandler("_eth", ethExt.Handler)

return hip5, nil
}

func (s *App) listen() error {
return s.server.ListenAndServe()
func (a *App) listen() error {
return a.server.ListenAndServe()
}

func (s *App) stop() {
s.proc.Stop()
s.server.Close()
s.server = &http.Server{
Addr: s.server.Addr,
Handler: s.server.Handler,
func (a *App) stop() {
a.proc.Stop()
a.server.Close()

// on stop create a new server
// to reset any state like old cache ... etc.
var err error
if a.server, err = a.newProxyServer(); err != nil {
log.Fatalf("app: error creating a new proxy server: %v", err)
}
}

func newProxy(c *config.App, proxyAddr string) (*http.Server, error) {
h, err := c.Proxy.NewHandler()
func (a *App) newProxyServer() (*http.Server, error) {
var err error

// add a new resolver to the proxy config
if a.config.Proxy.Resolver, err = a.NewResolver(); err != nil {
return nil, err
}

// initialize a new handler
h, err := a.config.Proxy.NewHandler()
if err != nil {
return nil, err
}
c.ProxyAddr = proxyAddr
server := &http.Server{Addr: c.ProxyAddr, Handler: h}

// copy proxy address from user specified config
a.config.ProxyAddr = a.usrConfig.ProxyAddr
server := &http.Server{Addr: a.config.ProxyAddr, Handler: h}
return server, nil
}

Expand Down

0 comments on commit 9cecd51

Please sign in to comment.