Skip to content

Commit 2ae166d

Browse files
committed
Read files from URLs
1 parent d64877e commit 2ae166d

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

cmd/enforcerd/main.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ package main
33
import (
44
"context"
55
"flag"
6+
"fmt"
7+
"io"
68
"io/ioutil"
79
"net"
10+
"net/http"
811
"os"
912
"strings"
1013

1114
"github.com/dhtech/dnsenforcer/enforcer"
1215
"github.com/dhtech/dnsenforcer/enforcer/ipplan"
13-
pb "github.com/dhtech/proto/dns"
16+
"github.com/ulikunitz/xz"
1417
log "github.com/sirupsen/logrus"
18+
pb "github.com/dhtech/proto/dns"
1519
"google.golang.org/grpc"
1620
"google.golang.org/grpc/reflection"
1721
"gopkg.in/yaml.v2"
@@ -26,18 +30,43 @@ type enforcerServer struct {
2630
}
2731

2832
func (s *enforcerServer) Refresh(ctx context.Context, req *pb.RefreshRequest) (*pb.RefreshResponse, error) {
29-
ipp, err := ipplan.Open("/etc/ipplan.db")
33+
ipplanXz, err := http.Get(os.Getenv("IPPLAN_XZ_URL"))
3034
if err != nil {
35+
log.Errorf("Unable to fetch IPPLAN_XZ_URL: %v", err)
36+
return nil, fmt.Errorf("failed to fetch ipplan.db.xz")
37+
}
38+
defer ipplanXz.Body.Close()
39+
40+
ipplanDb, err := xz.NewReader(ipplanXz.Body)
41+
if err != nil {
42+
log.Errorf("Failed to decompress ipplan.db: %v", err)
43+
return nil, fmt.Errorf("failed to decompress ipplan.db")
44+
}
45+
46+
ipplanFile, err := ioutil.TempFile("", "enforcerd")
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer os.Remove(ipplanFile.Name())
51+
if _, err := io.Copy(ipplanFile, ipplanDb); err != nil {
52+
log.Errorf("Failed to write ipplan.db: %v", err)
3153
return nil, err
3254
}
3355

34-
static, err := os.Open("./static.yml")
56+
ipp, err := ipplan.Open(ipplanFile.Name())
3557
if err != nil {
3658
return nil, err
3759
}
3860

61+
static, err := http.Get(os.Getenv("STATIC_URL"))
62+
if err != nil {
63+
log.Errorf("Unable to fetch STATIC_URL: %v", err)
64+
return nil, fmt.Errorf("failed to fetch static record map")
65+
}
66+
defer static.Body.Close()
67+
3968
// Create new enforcer
40-
e, err := enforcer.New(s.v, ipp, static)
69+
e, err := enforcer.New(s.v, ipp, static.Body)
4170
defer e.Close()
4271
if err != nil {
4372
return nil, err

0 commit comments

Comments
 (0)