diff options
author | sjlleo <[email protected]> | 2022-05-20 17:15:06 +0800 |
---|---|---|
committer | sjlleo <[email protected]> | 2022-05-20 17:15:06 +0800 |
commit | 5002df0f2bea39784a8ed128291bab424afc681a (patch) | |
tree | 29e6f15353f580565074ea0176a9384767bf994b /verify | |
parent | bdf72ae2308d40ca55666082f6e54891dad0c876 (diff) |
Refactor: Verify Module
Diffstat (limited to 'verify')
-rw-r--r-- | verify/ipv4.go | 72 | ||||
-rw-r--r-- | verify/ipv6.go | 73 | ||||
-rw-r--r-- | verify/verify.go | 73 | ||||
-rw-r--r-- | verify/verify_test.go | 13 |
4 files changed, 231 insertions, 0 deletions
diff --git a/verify/ipv4.go b/verify/ipv4.go new file mode 100644 index 0000000..89e0cc9 --- /dev/null +++ b/verify/ipv4.go @@ -0,0 +1,72 @@ +package verify + +import ( + "strconv" + + "github.com/sjlleo/netflix-verify/util" +) + +type IPv4Verifier struct { + Config + IP string + unblockStatus int + unblockTestChan chan UnblockTestResult +} + +func (v *IPv4Verifier) Execute() *VerifyResponse { + var err error + var response VerifyResponse + v.unblockStatus = AreaUnavailable + response.Type = 1 + + if v.IP, err = util.DnsResolver(4); err != nil { + return &VerifyResponse{} + } + + 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.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 *IPv4Verifier) upgradeStatus(status int) { + if v.unblockStatus < status { + v.unblockStatus = status + } +} + +func (v *IPv4Verifier) 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} + } +} diff --git a/verify/ipv6.go b/verify/ipv6.go new file mode 100644 index 0000000..07ff625 --- /dev/null +++ b/verify/ipv6.go @@ -0,0 +1,73 @@ +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 = -2 + 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.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} + } +} diff --git a/verify/verify.go b/verify/verify.go new file mode 100644 index 0000000..a657f3d --- /dev/null +++ b/verify/verify.go @@ -0,0 +1,73 @@ +package verify + +var AreaAvailableID = 80018499 +var SelfMadeAvailableID = 80197526 +var NonSelfMadeAvailableID = 70143836 + +const ( + AreaUnavailable = -1 + AreaAvailable = 0 + UnblockSelfMadeMovie = 1 + UnblockNonSelfMadeMovie = 2 +) + +type Verify interface { + Execute() *VerifyResponse +} + +type FinalResult struct { + Res map[int]VerifyResponse +} + +type VerifyResult struct { + Config + response chan *VerifyResponse +} + +type Config struct { + LocalAddr string + Custom string +} + +type VerifyResponse struct { + Type int + StatusCode int + CountryCode string + CountryName string +} + +type UnblockTestResult struct { + movieID int + CountryCode string + err error +} + +func NewVerify(c Config) *FinalResult { + var finalResult FinalResult + finalResult.Res = make(map[int]VerifyResponse) + + vr := VerifyResult{Config: c} + ch := make(chan *VerifyResponse) + vr.response = ch + defer close(ch) + + go vr.IPv4Verifier() + go vr.IPv6Verifier() + + for i := 0; i < 2; i++ { + if res, err := <-ch; err { + finalResult.Res[res.Type] = *res + } + } + return &finalResult +} + +func (vr *VerifyResult) IPv4Verifier() { + verify := &IPv4Verifier{Config: vr.Config} + vr.response <- verify.Execute() +} + +func (vr *VerifyResult) IPv6Verifier() { + verify := &IPv6Verifier{Config: vr.Config} + vr.response <- verify.Execute() +} diff --git a/verify/verify_test.go b/verify/verify_test.go new file mode 100644 index 0000000..6f334d4 --- /dev/null +++ b/verify/verify_test.go @@ -0,0 +1,13 @@ +package verify + +import "testing" + +func TestIPv4Execute(t *testing.T) { + verify := &IPv4Verifier{Config: Config{}} + verify.Execute() +} + +func TestIPv6Execute(t *testing.T) { + verify := &IPv6Verifier{Config: Config{}} + verify.Execute() +} |