summaryrefslogtreecommitdiff
path: root/verify/ipv6.go
blob: f6471b2d0428b2ef050e2a17beb5821c2b210539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package verify

import (
	"strconv"

	"github.com/sjlleo/netflix-verify/util"
)

type IPv6Verifier struct {
	Config
	IP              string
	unblockStatus   int
	unblockTestChan chan UnblockTestResult
}

func (v *IPv6Verifier) Execute() *VerifyResponse {
	var err error
	var response VerifyResponse
	v.unblockStatus = AreaUnavailable
	response.Type = 2

	if v.IP, err = util.DnsResolver(6); err != nil {
		response.StatusCode = NetworkUnrachable
		return &response
	}

	v.unblockTestChan = make(chan UnblockTestResult)
	defer close(v.unblockTestChan)

	go v.UnblockTest(AreaAvailableID)
	go v.UnblockTest(SelfMadeAvailableID)
	go v.UnblockTest(NonSelfMadeAvailableID)

	for i := 0; i < 3; i++ {
		switch res := <-v.unblockTestChan; {

		case res.err != nil:
			response.StatusCode = NetworkUnrachable

		case res.CountryCode != "":
			switch res.movieID {
			case AreaAvailableID:
				v.upgradeStatus(AreaAvailable)
			case SelfMadeAvailableID:
				v.upgradeStatus(UnblockSelfMadeMovie)
			case NonSelfMadeAvailableID:
				v.upgradeStatus(UnblockNonSelfMadeMovie)
			}
			response.CountryCode = res.CountryCode
			response.CountryName = util.CountryCodeToCountryName(res.CountryCode)
		default:
		}
	}
	response.StatusCode = v.unblockStatus
	return &response
}

func (v *IPv6Verifier) upgradeStatus(status int) {
	if v.unblockStatus < status {
		v.unblockStatus = status
	}
}

func (v *IPv6Verifier) UnblockTest(MoiveID int) {

	testURL := util.Netflix + strconv.Itoa(MoiveID)
	if reCode, err := util.RequestIP(testURL, v.IP, v.LocalAddr); err != nil {
		if err.Error() == "Banned" {
			v.unblockTestChan <- UnblockTestResult{MoiveID, "", nil}
		} else {
			v.unblockTestChan <- UnblockTestResult{MoiveID, "", err}
		}
	} else {
		v.unblockTestChan <- UnblockTestResult{MoiveID, reCode, nil}
	}
}