From 75dd2ad33db00b035b536d91eb0faf69f7eda771 Mon Sep 17 00:00:00 2001 From: yoneyan Date: Sun, 29 Aug 2021 03:39:08 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20=E7=99=BB=E9=8C=B2=E6=83=85=E5=A0=B1?= =?UTF-8?q?=E8=A9=B3=E7=B4=B0=E3=81=AE=E5=8F=96=E5=BE=97=E9=83=A8=E5=88=86?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interface.go | 61 ++-- jpnic.go | 196 ++++--------- jpnic_test.go | 759 +++----------------------------------------------- 3 files changed, 113 insertions(+), 903 deletions(-) diff --git a/interface.go b/interface.go index 841f982..e4e8d3e 100644 --- a/interface.go +++ b/interface.go @@ -99,22 +99,25 @@ type Etc struct { Password string `json:"password"` } -type InfoIPv4 struct { - IPAddress string `json:"ip_address"` - DetailLink string `json:"detail_link"` - Size string `json:"size"` - NetworkName string `json:"network_name"` - AssignDate string `json:"assign_date"` - ReturnDate string `json:"return_date"` - OrgName string `json:"org_name"` - Ryakusho string `json:"ryakusho"` - RecepNo string `json:"recep_no"` - DeliNo string `json:"deli_no"` - Type string `json:"type"` - KindID string `json:"kind_id"` +type InfoDetailShort struct { + IPAddress string `json:"ip_address"` + Ryakusho string `json:"ryakusho"` + Type string `json:"type"` + InfraUserKind string `json:"infra_user_kind"` + NetworkName string `json:"network_name"` + Org string `json:"org"` + OrgEn string `json:"org_en"` + AdminJPNICHandle string `json:"admin_jpnic_handle"` + AdminJPNICHandleLink string `json:"admin_jpnic_handle_link"` + TechJPNICHandle string `json:"tech_jpnic_handle"` + TechJPNICHandleLink string `json:"tech_jpnic_handle_link"` + NameServer string `json:"name_server"` + AssignDate string `json:"assign_date"` + ReturnDate string `json:"return_date"` + UpdateDate string `json:"update_date"` } -type InfoDetailIPv4 struct { +type InfoDetailLong struct { IPAddress string `json:"ip_address"` Ryakusho string `json:"ryakusho"` Type string `json:"type"` @@ -139,9 +142,10 @@ type InfoDetailIPv4 struct { UpdateDate string `json:"update_date"` } -type InfoIPv6 struct { +type InfoIPv4 struct { IPAddress string `json:"ip_address"` DetailLink string `json:"detail_link"` + Size string `json:"size"` NetworkName string `json:"network_name"` AssignDate string `json:"assign_date"` ReturnDate string `json:"return_date"` @@ -149,22 +153,19 @@ type InfoIPv6 struct { Ryakusho string `json:"ryakusho"` RecepNo string `json:"recep_no"` DeliNo string `json:"deli_no"` + Type string `json:"type"` KindID string `json:"kind_id"` } -type InfoDetailIPv6 struct { - IPAddress string `json:"ip_address"` - Ryakusho string `json:"ryakusho"` - Type string `json:"type"` - InfraUserKind string `json:"infra_user_kind"` - NetworkName string `json:"network_name"` - Org string `json:"org"` - OrgEn string `json:"org_en"` - AdminJPNICHandle string `json:"admin_jpnic_handle"` - AdminJPNICHandleLink string `json:"admin_jpnic_handle_link"` - TechJPNICHandle string `json:"tech_jpnic_handle"` - TechJPNICHandleLink string `json:"tech_jpnic_handle_link"` - AssignDate string `json:"assign_date"` - ReturnDate string `json:"return_date"` - UpdateDate string `json:"update_date"` +type InfoIPv6 struct { + IPAddress string `json:"ip_address"` + DetailLink string `json:"detail_link"` + NetworkName string `json:"network_name"` + AssignDate string `json:"assign_date"` + ReturnDate string `json:"return_date"` + OrgName string `json:"org_name"` + Ryakusho string `json:"ryakusho"` + RecepNo string `json:"recep_no"` + DeliNo string `json:"deli_no"` + KindID string `json:"kind_id"` } diff --git a/jpnic.go b/jpnic.go index afedf57..4d50959 100644 --- a/jpnic.go +++ b/jpnic.go @@ -418,8 +418,9 @@ func (c *Config) GetAllIPv6(searchStr string) ([]InfoIPv6, error) { return infos, nil } -func (c *Config) GetIPv4User(userURL string) (InfoDetailIPv4, error) { - var info InfoDetailIPv4 +func (c *Config) GetIPUser(userURL string) (InfoDetailLong, error) { + var info InfoDetailLong + var infoShort InfoDetailShort sessionID, err := randomStr() if err != nil { @@ -511,36 +512,61 @@ func (c *Config) GetIPv4User(userURL string) (InfoDetailIPv4, error) { tableCell3.Find("table").Each(func(_ int, tableHtml4 *goquery.Selection) { tableHtml4.Find("tr").Each(func(_ int, rowHtml4 *goquery.Selection) { rowHtml4.Find("td").Each(func(index int, tableCell4 *goquery.Selection) { - dataStr := strings.TrimSpace(string(tableCell4.Text())) - + dataStr := strings.TrimSpace(tableCell4.Text()) if index == 1 { switch count { case 0: info.IPAddress = dataStr + //short + infoShort.IPAddress = dataStr case 1: info.Ryakusho = dataStr + //short + infoShort.Ryakusho = dataStr case 2: info.Type = dataStr + //short + infoShort.Type = dataStr case 3: info.InfraUserKind = dataStr + //short + infoShort.InfraUserKind = dataStr case 4: info.NetworkName = dataStr + //short + infoShort.NetworkName = dataStr case 5: info.Org = dataStr + //short + infoShort.Org = dataStr case 6: info.OrgEn = dataStr + //short + infoShort.OrgEn = dataStr case 7: info.PostCode = dataStr + //short + infoShort.AdminJPNICHandle = dataStr + infoShort.AdminJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") case 8: info.Address = dataStr + //short + infoShort.TechJPNICHandle = dataStr + infoShort.TechJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") case 9: info.AddressEn = dataStr + //short + infoShort.AssignDate = dataStr case 10: info.AdminJPNICHandle = dataStr info.AdminJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") + //short + infoShort.ReturnDate = dataStr case 11: info.TechJPNICHandle = dataStr info.TechJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") + //short + infoShort.UpdateDate = dataStr case 12: info.NameServer = dataStr case 13: @@ -562,148 +588,26 @@ func (c *Config) GetIPv4User(userURL string) (InfoDetailIPv4, error) { } }) }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - - return info, err -} - -func (c *Config) GetIPv6User(userURL string) (InfoDetailIPv6, error) { - var info InfoDetailIPv6 - - sessionID, err := randomStr() - if err != nil { - return info, err - } - - cert, err := tls.LoadX509KeyPair(c.CertFilePath, c.KeyFilePath) - if err != nil { - return info, err - } - - // Load CA - caCert, err := ioutil.ReadFile(c.CAFilePath) - if err != nil { - return info, err - } - caCertPool := x509.NewCertPool() - caCertPool.AppendCertsFromPEM(caCert) - - tlsConfig := &tls.Config{ - Certificates: []tls.Certificate{cert}, - RootCAs: caCertPool, - } - tlsConfig.BuildNameToCertificate() - transport := &http.Transport{TLSClientConfig: tlsConfig} - - cookies := []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - urlObj, _ := url.Parse("https://iphostmaster.nic.ad.jp/") - jar, err := cookiejar.New(nil) - if err != nil { - return info, err - } - - jar.SetCookies(urlObj, cookies) - - client := &http.Client{Transport: transport, Jar: jar} - - resp, err := client.Get("https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do") - if err != nil { - return info, err - } - defer resp.Body.Close() - - sessionID = resp.Header.Get("Set-Cookie")[11:43] - - cookies = []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - jar.SetCookies(urlObj, cookies) - - resp, err = client.Get("https://iphostmaster.nic.ad.jp" + userURL) - if err != nil { - return info, err - } - defer resp.Body.Close() - - reader := transform.NewReader(resp.Body, japanese.ShiftJIS.NewDecoder()) - bodyByte, err := ioutil.ReadAll(reader) - if err != nil { - return info, err - } - - doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyByte))) - if err != nil { - return info, err - } - - count := 0 - - doc.Find("table").Each(func(_ int, tableHtml1 *goquery.Selection) { - tableHtml1.Find("tr").Each(func(_ int, rowHtml1 *goquery.Selection) { - rowHtml1.Find("td").Each(func(_ int, tableCell1 *goquery.Selection) { - tableCell1.Find("table").Each(func(_ int, tableHtml2 *goquery.Selection) { - tableHtml2.Find("tr").Each(func(_ int, rowHtml2 *goquery.Selection) { - rowHtml2.Find("td").Each(func(_ int, tableCell2 *goquery.Selection) { - tableCell2.Find("table").Each(func(_ int, tableHtml3 *goquery.Selection) { - tableHtml3.Find("tr").Each(func(_ int, rowHtml3 *goquery.Selection) { - rowHtml3.Find("td").Each(func(_ int, tableCell3 *goquery.Selection) { - tableCell3.Find("table").Each(func(_ int, tableHtml4 *goquery.Selection) { - tableHtml4.Find("tr").Each(func(_ int, rowHtml4 *goquery.Selection) { - rowHtml4.Find("td").Each(func(index int, tableCell4 *goquery.Selection) { - dataStr := strings.TrimSpace(tableCell4.Text()) - - if index == 1 { - switch count { - case 0: - info.IPAddress = dataStr - case 1: - info.Ryakusho = dataStr - case 2: - info.Type = dataStr - case 3: - info.InfraUserKind = dataStr - case 4: - info.NetworkName = dataStr - case 5: - info.Org = dataStr - case 6: - info.OrgEn = dataStr - case 7: - info.AdminJPNICHandle = dataStr - info.AdminJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") - case 8: - info.TechJPNICHandle = dataStr - info.TechJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") - case 9: - info.AssignDate = dataStr - case 10: - info.ReturnDate = dataStr - case 11: - info.UpdateDate = dataStr - } - count++ - } - }) - }) + // 自所属ではないとき、struct数が13個になる + // この場合は、並び替えを実施する + if count == 12 { + info = InfoDetailLong{} + info.IPAddress = infoShort.IPAddress + info.Ryakusho = infoShort.Ryakusho + info.Type = infoShort.Type + info.InfraUserKind = infoShort.InfraUserKind + info.NetworkName = infoShort.NetworkName + info.Org = infoShort.Org + info.OrgEn = infoShort.OrgEn + info.AdminJPNICHandle = infoShort.AdminJPNICHandle + info.AdminJPNICHandleLink = infoShort.AdminJPNICHandleLink + info.TechJPNICHandle = infoShort.TechJPNICHandle + info.TechJPNICHandleLink = infoShort.TechJPNICHandleLink + info.NameServer = infoShort.NameServer + info.AssignDate = infoShort.AssignDate + info.ReturnDate = infoShort.ReturnDate + info.UpdateDate = infoShort.UpdateDate + } }) }) }) diff --git a/jpnic_test.go b/jpnic_test.go index 966b360..f32276f 100644 --- a/jpnic_test.go +++ b/jpnic_test.go @@ -1,789 +1,94 @@ package jpnic import ( - "bytes" - "crypto/tls" - "crypto/x509" - "github.com/PuerkitoBio/goquery" - "golang.org/x/text/encoding/japanese" - "golang.org/x/text/transform" - "io/ioutil" - "log" - "net/http" - "net/http/cookiejar" - "net/url" - "strings" "testing" ) +var caFilePath = "/home/yonedayuto/Documents/HomeNOC/cert/rootcacert_r3.cer" + // HomeNOC -//var certFilePath = "/home/yonedayuto/Documents/HomeNOC/cert/v4-cert.pem" -//var keyFilePath = "/home/yonedayuto/Documents/HomeNOC/cert/v4-prvkey.pem" -//var caFilePath = "/home/yonedayuto/Documents/HomeNOC/cert/rootcacert_r3.cer" +//var certFilePathV4 = "/home/yonedayuto/Documents/HomeNOC/cert/v4-cert.pem" +//var keyFilePathV4 = "/home/yonedayuto/Documents/HomeNOC/cert/v4-prvkey.pem" +//var certFilePathV6 = "/home/yonedayuto/Documents/HomeNOC/cert/v6-cert.pem" +//var keyFilePathV6 = "/home/yonedayuto/Documents/HomeNOC/cert/v6-prvkey.pem" // doornoc -var certFilePath = "/home/yonedayuto/Documents/doornoc/cert/v4-cert.pem" -var keyFilePath = "/home/yonedayuto/Documents/doornoc/cert/v4-prvkey.pem" -var caFilePath = "/home/yonedayuto/Documents/doornoc/cert/rootcacert_r3.cer" - -// Search String -var searchStr = "doornoc" +var certFilePathV4 = "/home/yonedayuto/Documents/doornoc/cert/v4-cert.pem" +var keyFilePathV4 = "/home/yonedayuto/Documents/doornoc/cert/v4-prvkey.pem" +var certFilePathV6 = "/home/yonedayuto/Documents/doornoc/cert/v6-cert.pem" +var keyFilePathV6 = "/home/yonedayuto/Documents/doornoc/cert/v6-prvkey.pem" -//func TestResultProcess(t *testing.T) { -// var result Result -// -// str := "\n\nRET=00\nRET_CODE=00000000\nRECEP_NO=001\nRECEP_HSNO=0\nADM_JPNIC_HDL=JP00\nADM_GNAME_JP=Y-Net\nTECH1_JPNIC_HDL=JP00\nTECH1_GNAME_JP=Y-Net\nTECH2_JPNIC_HDL=\nTECH2_GNAME_JP=\nCOUNT=0\n\n" -// reader := strings.NewReader(str) -// scanner := bufio.NewScanner(reader) -// -// success := false -// var retCode []string -// ret := "00" -// -// for scanner.Scan() { -// // RET -// if strings.Contains(scanner.Text(), "RET=") { -// t.Logf("Error: %s", scanner.Text()[4:]) -// ret = scanner.Text()[4:] -// if scanner.Text()[4:] == "00" { -// success = true -// } -// ret = scanner.Text()[4:] -// } -// -// // RET_CODE -// if strings.Contains(scanner.Text(), "RET_CODE=") { -// t.Logf("RET_CODE Error: %s", scanner.Text()[9:]) -// retCode = append(retCode, scanner.Text()[9:]) -// } -// -// // RECEP_NO -// if strings.Contains(scanner.Text(), "RECEP_NO=") { -// t.Logf("RECEP_NO=%s", scanner.Text()[9:]) -// result.RecepNo = scanner.Text()[9:] -// } -// -// // Admin -// if strings.Contains(scanner.Text(), "ADM_JPNIC_HDL=") { -// t.Logf("ADM_JPNIC_HDL=%s", scanner.Text()[14:]) -// result.AdmJPNICHdl = scanner.Text()[14:] -// } -// -// // Tech1 -// if strings.Contains(scanner.Text(), "TECH1_JPNIC_HDL=") { -// t.Logf("TECH1_JPNIC_HDL=%s", scanner.Text()[16:]) -// result.Tech1JPNICHdl = scanner.Text()[16:] -// } -// -// // Tech2 -// if strings.Contains(scanner.Text(), "TECH2_JPNIC_HDL=") { -// t.Logf("TECH2_JPNIC_HDL=%s", scanner.Text()[16:]) -// result.Tech2JPNICHdl = scanner.Text()[16:] -// } -// -// t.Log(scanner.Text()) -// } -// -// // RET -// if ret != "00" { -// code, _ := strconv.Atoi(ret) -// ErrorStatusText(code) -// } -// -// // RET_CODE -// for _, code := range retCode { -// var errStr string -// t.Logf("%s", code[4:7]) -// -// // interface -// if code[4:7] == "000" { -// code, _ := strconv.Atoi(code[4:7]) -// errStr += ErrorStatusText(code) -// -// } -// -// // error genre -// if code[7:] != "0" { -// code, _ := strconv.Atoi(code[7:]) -// errStr += "_" + ErrorStatusText(code) -// } -// } -// t.Log(success) -//} +//var caFilePath = "/home/yonedayuto/Documents/doornoc/cert/rootcacert_r3.cer" -//func TestSend(t *testing.T) { -// con := Config{ -// URL: "https://iphostmaster.nic.ad.jp/webtrans/WebRegisterCtl", -// CertFilePath: certFilePath, -// KeyFilePath: keyFilePath, -// CAFilePath: caFilePath, -// } -// input := WebTransaction{} -// -// err := con.Send(input) -// if err.Err != nil { -// t.Log(err) -// } -//} - -func Test1GetIPv4(t *testing.T) { - con := Config{ - URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: certFilePath, - KeyFilePath: keyFilePath, - CAFilePath: caFilePath, - } - - data, err := con.GetAllIPv4(searchStr) - if err != nil { - t.Fatal(err) - } - t.Log(data) -} +// Search String (HOMENOC/DOORNOC) +var searchStr = "HOMENOC" +var v4UserURL = "/jpnic/entryinfo_v4.do?netwrk_id=2020021426910" +var v6UserURL = "/jpnic/G11320.do?netwrk_id=2020021427992" func TestGetIPv4(t *testing.T) { - sessionID, err := randomStr() - if err != nil { - t.Fatal(err) - } - con := Config{ URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: certFilePath, - KeyFilePath: keyFilePath, + CertFilePath: certFilePathV4, + KeyFilePath: keyFilePathV4, CAFilePath: caFilePath, } - cert, err := tls.LoadX509KeyPair(con.CertFilePath, con.KeyFilePath) - if err != nil { - t.Fatal(err) - } - - // Load CA - caCert, err := ioutil.ReadFile(con.CAFilePath) - if err != nil { - t.Fatal(err) - } - caCertPool := x509.NewCertPool() - caCertPool.AppendCertsFromPEM(caCert) - - tlsConfig := &tls.Config{ - Certificates: []tls.Certificate{cert}, - RootCAs: caCertPool, - } - tlsConfig.BuildNameToCertificate() - transport := &http.Transport{TLSClientConfig: tlsConfig} - - cookies := []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - urlObj, _ := url.Parse("https://iphostmaster.nic.ad.jp/") - jar, err := cookiejar.New(nil) - if err != nil { - t.Fatal(err) - } - - jar.SetCookies(urlObj, cookies) - - client := &http.Client{Transport: transport, Jar: jar} - - resp, err := client.Get(con.URL) - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - sessionID = resp.Header.Get("Set-Cookie")[11:43] - - cookies = []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - jar.SetCookies(urlObj, cookies) - - resp, err = client.Get("https://iphostmaster.nic.ad.jp/jpnic/portalv4list.do") - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - contentType := "application/x-www-form-urlencoded" - - str := "destdisp=D12204&ipaddr=&sizeS=&sizeE=&netwrkName=®DateS=®DateE=&rtnDateS=&rtnDateE=&organizationName=&resceAdmSnm=" + searchStr + "&recepNo=&deliNo=" - // utf-8 => shift-jis - iostr := strings.NewReader(str) - rio := transform.NewReader(iostr, japanese.ShiftJIS.NewEncoder()) - strByte, err := ioutil.ReadAll(rio) - if err != nil { - t.Fatal(err) - } - - resp, err = client.Post("https://iphostmaster.nic.ad.jp/jpnic/portalv4listmain.do", contentType, bytes.NewBuffer(strByte)) - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - bodyStr, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr))) + data, err := con.GetAllIPv4(searchStr) if err != nil { t.Fatal(err) } - count := 0 - var infos []InfoIPv4 - - doc.Find("table").Each(func(_ int, tableHtml *goquery.Selection) { - tableHtml.Find("tr").Each(func(_ int, rowHtml *goquery.Selection) { - var info InfoIPv4 - rowHtml.Find("td").Each(func(index int, tableCell *goquery.Selection) { - ioStr := strings.NewReader(tableCell.Text()) - reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) - strByte, err := ioutil.ReadAll(reader) - if err != nil { - t.Fatal(err) - } - dataStr := strings.TrimSpace(string(strByte)) - - switch index { - case 0: - info.IPAddress = dataStr - info.DetailLink, _ = tableCell.Find("a").Attr("href") - case 1: - info.Size = dataStr - case 2: - info.NetworkName = dataStr - case 3: - info.AssignDate = dataStr - case 4: - info.ReturnDate = dataStr - case 5: - info.OrgName = dataStr - case 6: - info.Ryakusho = dataStr - case 7: - info.RecepNo = dataStr - case 8: - info.DeliNo = dataStr - case 9: - info.Type = dataStr - case 10: - info.KindID = dataStr - } - count++ - }) - if count == 11 { - infos = append(infos, info) - } - count = 0 - }) - }) - - infos = infos[1:] - - for _, tmp := range infos { + for _, tmp := range data { t.Log(tmp) - } -} -func Test1GetIPv6(t *testing.T) { - con := Config{ - URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: certFilePath, - KeyFilePath: keyFilePath, - CAFilePath: caFilePath, } - - data, err := con.GetAllIPv6(searchStr) - if err != nil { - t.Fatal(err) - } - t.Log(data) } func TestGetIPv6(t *testing.T) { - sessionID, err := randomStr() - if err != nil { - t.Fatal(err) - } - con := Config{ URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: certFilePath, - KeyFilePath: keyFilePath, + CertFilePath: certFilePathV6, + KeyFilePath: keyFilePathV6, CAFilePath: caFilePath, } - cert, err := tls.LoadX509KeyPair(con.CertFilePath, con.KeyFilePath) - if err != nil { - t.Fatal(err) - } - - // Load CA - caCert, err := ioutil.ReadFile(con.CAFilePath) - if err != nil { - t.Fatal(err) - } - caCertPool := x509.NewCertPool() - caCertPool.AppendCertsFromPEM(caCert) - - tlsConfig := &tls.Config{ - Certificates: []tls.Certificate{cert}, - RootCAs: caCertPool, - } - tlsConfig.BuildNameToCertificate() - transport := &http.Transport{TLSClientConfig: tlsConfig} - - cookies := []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - urlObj, _ := url.Parse("https://iphostmaster.nic.ad.jp/") - jar, err := cookiejar.New(nil) - if err != nil { - t.Fatal(err) - } - - jar.SetCookies(urlObj, cookies) - - client := &http.Client{Transport: transport, Jar: jar} - - resp, err := client.Get(con.URL) - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - sessionID = resp.Header.Get("Set-Cookie")[11:43] - - cookies = []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - jar.SetCookies(urlObj, cookies) - - contentType := "application/x-www-form-urlencoded" - - str := "destdisp=D12204&ipaddr=&sizeS=&sizeE=&netwrkName=®DateS=®DateE=&rtnDateS=&rtnDateE=&organizationName=&resceAdmSnm=" + searchStr + "&recepNo=&deliNo=" - // utf-8 => shift-jis - iostr := strings.NewReader(str) - rio := transform.NewReader(iostr, japanese.ShiftJIS.NewEncoder()) - strByte, err := ioutil.ReadAll(rio) - if err != nil { - t.Fatal(err) - } - - resp, err = client.Post("https://iphostmaster.nic.ad.jp/jpnic/K11310Action.do", contentType, bytes.NewBuffer(strByte)) - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - bodyStr, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr))) + data, err := con.GetAllIPv4(searchStr) if err != nil { t.Fatal(err) } - count := 0 - var infos []InfoIPv6 - - doc.Find("table").Each(func(_ int, tableHtml *goquery.Selection) { - tableHtml.Find("tr").Each(func(_ int, rowHtml *goquery.Selection) { - var info InfoIPv6 - rowHtml.Find("td").Each(func(index int, tableCell *goquery.Selection) { - ioStr := strings.NewReader(tableCell.Text()) - reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) - strByte, err := ioutil.ReadAll(reader) - if err != nil { - t.Fatal(err) - } - dataStr := strings.TrimSpace(string(strByte)) - - switch index { - case 0: - info.IPAddress = dataStr - info.DetailLink, _ = tableCell.Find("a").Attr("href") - case 1: - info.NetworkName = dataStr - case 2: - info.AssignDate = dataStr - case 3: - info.ReturnDate = dataStr - case 4: - info.OrgName = dataStr - case 5: - info.Ryakusho = dataStr - case 6: - info.RecepNo = dataStr - case 7: - info.DeliNo = dataStr - case 8: - info.KindID = dataStr - } - count++ - }) - if count == 9 { - infos = append(infos, info) - } - count = 0 - }) - }) - - infos = infos[1:] - - for _, tmp := range infos { + for _, tmp := range data { t.Log(tmp) } } func TestGetIPv4User(t *testing.T) { - sessionID, err := randomStr() - if err != nil { - t.Fatal(err) - } - con := Config{ - URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: certFilePath, - KeyFilePath: keyFilePath, + CertFilePath: certFilePathV4, + KeyFilePath: keyFilePathV4, CAFilePath: caFilePath, } - cert, err := tls.LoadX509KeyPair(con.CertFilePath, con.KeyFilePath) - if err != nil { - t.Fatal(err) - } - - // Load CA - caCert, err := ioutil.ReadFile(con.CAFilePath) - if err != nil { - t.Fatal(err) - } - caCertPool := x509.NewCertPool() - caCertPool.AppendCertsFromPEM(caCert) - - tlsConfig := &tls.Config{ - Certificates: []tls.Certificate{cert}, - RootCAs: caCertPool, - } - tlsConfig.BuildNameToCertificate() - transport := &http.Transport{TLSClientConfig: tlsConfig} - - cookies := []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - urlObj, _ := url.Parse("https://iphostmaster.nic.ad.jp/") - jar, err := cookiejar.New(nil) - if err != nil { - t.Fatal(err) - } - - jar.SetCookies(urlObj, cookies) - - client := &http.Client{Transport: transport, Jar: jar} - - resp, err := client.Get(con.URL) - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - sessionID = resp.Header.Get("Set-Cookie")[11:43] - - cookies = []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - jar.SetCookies(urlObj, cookies) - - resp, err = client.Get("https://iphostmaster.nic.ad.jp/jpnic/entryinfo_v4.do?netwrk_id=2020021426910") - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - bodyStr, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr))) + data, err := con.GetIPUser(v4UserURL) if err != nil { t.Fatal(err) } - count := 0 - var info InfoDetailIPv4 - - doc.Find("table").Each(func(_ int, tableHtml1 *goquery.Selection) { - tableHtml1.Find("tr").Each(func(_ int, rowHtml1 *goquery.Selection) { - rowHtml1.Find("td").Each(func(_ int, tableCell1 *goquery.Selection) { - tableCell1.Find("table").Each(func(_ int, tableHtml2 *goquery.Selection) { - tableHtml2.Find("tr").Each(func(_ int, rowHtml2 *goquery.Selection) { - rowHtml2.Find("td").Each(func(_ int, tableCell2 *goquery.Selection) { - tableCell2.Find("table").Each(func(_ int, tableHtml3 *goquery.Selection) { - tableHtml3.Find("tr").Each(func(_ int, rowHtml3 *goquery.Selection) { - rowHtml3.Find("td").Each(func(_ int, tableCell3 *goquery.Selection) { - tableCell3.Find("table").Each(func(_ int, tableHtml4 *goquery.Selection) { - tableHtml4.Find("tr").Each(func(_ int, rowHtml4 *goquery.Selection) { - rowHtml4.Find("td").Each(func(index int, tableCell4 *goquery.Selection) { - ioStr := strings.NewReader(tableCell4.Text()) - reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) - strByte, err := ioutil.ReadAll(reader) - if err != nil { - t.Fatal(err) - } - dataStr := strings.TrimSpace(string(strByte)) - - if index == 1 { - switch count { - case 0: - info.IPAddress = dataStr - case 1: - info.Ryakusho = dataStr - case 2: - info.Type = dataStr - case 3: - info.InfraUserKind = dataStr - case 4: - info.NetworkName = dataStr - case 5: - info.Org = dataStr - case 6: - info.OrgEn = dataStr - case 7: - info.PostCode = dataStr - case 8: - info.Address = dataStr - case 9: - info.AddressEn = dataStr - case 10: - info.AdminJPNICHandle = dataStr - info.AdminJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") - case 11: - info.TechJPNICHandle = dataStr - info.TechJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") - case 12: - info.NameServer = dataStr - case 13: - info.DSRecord = dataStr - case 14: - info.NotifyAddress = dataStr - case 15: - info.DeliNo = dataStr - case 16: - info.RecepNo = dataStr - case 17: - info.AssignDate = dataStr - case 18: - info.ReturnDate = dataStr - case 19: - info.UpdateDate = dataStr - } - count++ - } - }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - - //infos = infos[1:] - - //for _, tmp := range infos { - t.Log(info) - //} + t.Log(data) } func TestGetIPv6User(t *testing.T) { - sessionID, err := randomStr() - if err != nil { - t.Fatal(err) - } - con := Config{ - URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: certFilePath, - KeyFilePath: keyFilePath, + CertFilePath: certFilePathV6, + KeyFilePath: keyFilePathV6, CAFilePath: caFilePath, } - cert, err := tls.LoadX509KeyPair(con.CertFilePath, con.KeyFilePath) - if err != nil { - t.Fatal(err) - } - - // Load CA - caCert, err := ioutil.ReadFile(con.CAFilePath) - if err != nil { - t.Fatal(err) - } - caCertPool := x509.NewCertPool() - caCertPool.AppendCertsFromPEM(caCert) - - tlsConfig := &tls.Config{ - Certificates: []tls.Certificate{cert}, - RootCAs: caCertPool, - } - tlsConfig.BuildNameToCertificate() - transport := &http.Transport{TLSClientConfig: tlsConfig} - - cookies := []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - urlObj, _ := url.Parse("https://iphostmaster.nic.ad.jp/") - jar, err := cookiejar.New(nil) - if err != nil { - t.Fatal(err) - } - - jar.SetCookies(urlObj, cookies) - - client := &http.Client{Transport: transport, Jar: jar} - - resp, err := client.Get(con.URL) - if err != nil { - t.Fatal(err) - } - defer resp.Body.Close() - - sessionID = resp.Header.Get("Set-Cookie")[11:43] - - cookies = []*http.Cookie{ - { - Name: "JSESSIONID", - Value: sessionID, - }, - } - - jar.SetCookies(urlObj, cookies) + t.Log(con) - resp, err = client.Get("https://iphostmaster.nic.ad.jp/jpnic/G11320.do?netwrk_id=2020021427992") + data, err := con.GetIPUser(v6UserURL) if err != nil { t.Fatal(err) } - defer resp.Body.Close() - - bodyStr, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - - doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr))) - if err != nil { - t.Fatal(err) - } - - count := 0 - var info InfoDetailIPv6 - - doc.Find("table").Each(func(_ int, tableHtml1 *goquery.Selection) { - tableHtml1.Find("tr").Each(func(_ int, rowHtml1 *goquery.Selection) { - rowHtml1.Find("td").Each(func(_ int, tableCell1 *goquery.Selection) { - tableCell1.Find("table").Each(func(_ int, tableHtml2 *goquery.Selection) { - tableHtml2.Find("tr").Each(func(_ int, rowHtml2 *goquery.Selection) { - rowHtml2.Find("td").Each(func(_ int, tableCell2 *goquery.Selection) { - tableCell2.Find("table").Each(func(_ int, tableHtml3 *goquery.Selection) { - tableHtml3.Find("tr").Each(func(_ int, rowHtml3 *goquery.Selection) { - rowHtml3.Find("td").Each(func(_ int, tableCell3 *goquery.Selection) { - tableCell3.Find("table").Each(func(_ int, tableHtml4 *goquery.Selection) { - tableHtml4.Find("tr").Each(func(_ int, rowHtml4 *goquery.Selection) { - rowHtml4.Find("td").Each(func(index int, tableCell4 *goquery.Selection) { - ioStr := strings.NewReader(tableCell4.Text()) - reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) - strByte, err := ioutil.ReadAll(reader) - if err != nil { - t.Fatal(err) - } - dataStr := strings.TrimSpace(string(strByte)) - - if index == 1 { - switch count { - case 0: - info.IPAddress = dataStr - case 1: - info.Ryakusho = dataStr - case 2: - info.Type = dataStr - case 3: - info.InfraUserKind = dataStr - case 4: - info.NetworkName = dataStr - case 5: - info.Org = dataStr - case 6: - info.OrgEn = dataStr - case 7: - info.AdminJPNICHandle = dataStr - info.AdminJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") - case 8: - info.TechJPNICHandle = dataStr - info.TechJPNICHandleLink, _ = tableCell4.Find("a").Attr("href") - case 9: - info.AssignDate = dataStr - case 10: - info.ReturnDate = dataStr - case 11: - info.UpdateDate = dataStr - } - count++ - } - }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - }) - - //infos = infos[1:] - - //for _, tmp := range infos { - t.Log(info) - //} + t.Log(data) }