Commit 4a85087c authored by Pratik Lagaskar's avatar Pratik Lagaskar
Browse files

Merge branch 'fixed-entry-point' into 'no-masters'

Fixed entry point for the library

See merge request !3
parents ffd02294 061a5902
Loading
Loading
Loading
Loading
+27 −4
Changes for README.md: 27 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -69,13 +69,18 @@ go mod tidy
If go.mod and go.sum are missing from the cloned repository, then:
```
# go mod init [module-path]
go mod init main
go mod init 0xacab.org/leap/vpn-hole/vpnhole
``` 

## Build & Run <a name="Build & Run"></a>

To build the library and use it as a standalone:
```
# navigate to app/vpnhole

cd app/vpnhole

# build the library

go build
```
An executable binary file would be generated for the library.
@@ -107,10 +112,10 @@ sudo systemctl disable systemd-resolved
To disable it permanently, uncomment and change `DNSStubListener` to *no* in `/etc/systemd/resolved.conf`; you can revert back everything to default if you stop using the vpn-hole library as a standalone.
Also stop any other service which prevents from listening on port 53. Then, you can simply execute the binary file.

In **Windows**, an executable named- main.exe would be created. 
In **Windows**, an executable named- vpnhole.exe would be created. 
```
# executing the binary
main.exe
vpnhole.exe
```
This would start the network wide DNS-level blocker on port 53.

@@ -134,12 +139,30 @@ Addresses: ::
## Integration <a name="Integration"></a>
To integrate the VPN-Hole Library with existing cross-platform applications, we will be using gomobile to compile and bind it.
```
# For binding with Android Platforms:

cd vpn-hole/vpnhole

# use gomobile to generate .aar:

gomobile bind -target='android' -o path/for/the/output/filename.aar -v path/for/vpnhole.go

For example: 
# Here pwd is path/vpn-hole/vpnhole

gomobile bind -target='android' -o vpn-hole.aar -v

# Similarly, GoMobile can be used to compile for other platforms like iOS (XCode is required):

gomobile bind -target='ios' -o path/for/the/output/filename.framework -v path/for/vpnhole.go

```

### Adapting to Bitmask_core <a name="Adapting to Bitmask_core"></a>
```



```

## License <a name="License"></a>
+8 −4
Changes for app/vpnhole/blocklist.go: 8 added lines, 4 removed lines.
Original line number Diff line number Diff line
package main

import (
	"net/http"
	"strings"
	"main/blacklist"
	"time"

	"0xacab.org/leap/vpn-hole/blacklist"
	"github.com/miekg/dns"
)

var privBlacklist = blacklist.New(httpClient)
var httpClient = &http.Client{Timeout: 30 * time.Second}
var PrivBlacklist = blacklist.New(httpClient)

func isBlacklisted(req *dns.Msg) bool {
func IsBlacklisted(req *dns.Msg) bool {
	if req.Opcode != dns.OpcodeQuery {
		return false
	}
@@ -26,5 +30,5 @@ func isBlacklisted(req *dns.Msg) bool {
		return false
	}

	return privBlacklist.Contains(strings.TrimRight(q.Name, "."))
	return PrivBlacklist.Contains(strings.TrimRight(q.Name, "."))
}
+10 −10
Changes for app/vpnhole/handler.go: 10 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -5,41 +5,41 @@ import (
	"log"
	"net"

	"0xacab.org/leap/vpn-hole/vpnhole/vpnhole"
	"github.com/miekg/dns"
)

var (
	client dns.Client
	upstream string

	blockIPv4 = net.ParseIP("0.0.0.0")
	blockIPv6 = net.ParseIP("0:0:0:0:0:0:0:0")
	blockTTL  = uint32(60)
)

func handler(rw dns.ResponseWriter, req *dns.Msg) {
func Handler(rw dns.ResponseWriter, req *dns.Msg) {
	defer rw.Close()

	if isBlacklisted(req) {
		if err := block(rw, req); err != nil {
			log.Println(fmt.Errorf("Failed to block request: %w", err))
	if IsBlacklisted(req) {
		if err := Block(rw, req); err != nil {
			log.Println(fmt.Errorf("failed to block request: %w", err))
		}

		return
	}

	resp, _, err := client.Exchange(req, upstream)
	c := alter.ParseFlags()
	resp, _, err := client.Exchange(req, c.Upstream)
	if err != nil {
		log.Fatalln(fmt.Errorf("Failed to exchange: %w", err))
		log.Fatalln(fmt.Errorf("failed to exchange: %w", err))
		return
	}

	if err = rw.WriteMsg(resp); err != nil {
		log.Println(fmt.Errorf("Failed to reply: %w", err))
		log.Println(fmt.Errorf("failed to reply: %w", err))
	}
}

func block(rw dns.ResponseWriter, req *dns.Msg) error {
func Block(rw dns.ResponseWriter, req *dns.Msg) error {
	resp := &dns.Msg{}
	resp.SetReply(req)

+57
Changes for app/vpnhole/main.go: 57 added lines, 0 removed lines.
Original line number Diff line number Diff line
package main

import (
	"context"
	"fmt"
	"log"

	"0xacab.org/leap/vpn-hole/vpnhole/vpnhole"
	"github.com/miekg/dns"

	"time"
)

func init() {
	// start the cmdline parser
	alter.ParseFlags()

}

func main() {

	// call ParseFlags() to get the config struct with the values
	c := alter.ParseFlags()
	fmt.Println(c)

	// start the vpnhole
	if err := c.Start(); err != nil {
		log.Fatalln(fmt.Errorf("failed to start vpnhole: %w", err))
	}
	defer func() {
		if err := c.Stop(); err != nil {
			log.Println(fmt.Errorf("failed to stop vpnhole: %w", err))
		}
	}()
	log.Printf("vpnhole started: %s", c)

	subscriptions, err := ReadSubscriptions(c.SubscriptionsFilename)
	if err != nil {
		log.Fatalln(fmt.Errorf("failed to read subscriptions list: %w", err))
	}

	for _, blacklistURL := range subscriptions {
		PrivBlacklist.Subscribe(blacklistURL)
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	go PrivBlacklist.Watch(ctx, time.Minute*10)

	dns.HandleFunc(".", Handler)

	if err = dns.ListenAndServe(c.Addr, "udp", nil); err != nil {
		log.Println(fmt.Errorf("failed to serve DNS server: %w", err))
	}

}
+1 −1
Changes for app/vpnhole/sublist.go: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ import (
	"os"
)

func readSubscriptions(filename string) ([]string, error) {
func ReadSubscriptions(filename string) ([]string, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, fmt.Errorf("failed to open: %w", err)
Loading