From 8380aff28d49c527161766b78782fc89cf0eb6cc Mon Sep 17 00:00:00 2001 From: yoneyan Date: Sat, 28 Aug 2021 04:10:06 +0900 Subject: [PATCH] [support] Supported getting group data. --- README.md | 2 + interface.go | 42 ++++ jpnic.go | 598 ++++++++++++++++++++++++++++++++++++++++++++++++++ jpnic_test.go | 439 ++++++++++++++++++++++++++++++------ 4 files changed, 1008 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 6b64604..d9b32f4 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ - IPv4変更 - IPv6登録 - IPv6変更 +- IPv4情報の閲覧 +- IPv6情報の閲覧 削除はJPNIC側のAPIサーバ側が対応していないため、対応不可。 また、詳しい仕様に関してはJPNIC側のトランザクション資料と照らし合わせながら使う必要があります。 diff --git a/interface.go b/interface.go index 6c4637b..841f982 100644 --- a/interface.go +++ b/interface.go @@ -114,6 +114,31 @@ type InfoIPv4 struct { KindID string `json:"kind_id"` } +type InfoDetailIPv4 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"` + PostCode string `json:"post_code"` + Address string `json:"address"` + AddressEn string `json:"address_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"` + DSRecord string `json:"ds_record"` + NotifyAddress string `json:"notify_address"` + DeliNo string `json:"deli_no"` + RecepNo string `json:"recep_no"` + 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"` @@ -126,3 +151,20 @@ type InfoIPv6 struct { DeliNo string `json:"deli_no"` 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"` +} diff --git a/jpnic.go b/jpnic.go index 3b055a7..bd522d7 100644 --- a/jpnic.go +++ b/jpnic.go @@ -6,10 +6,14 @@ import ( "crypto/tls" "crypto/x509" "fmt" + "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" "strconv" "strings" ) @@ -140,3 +144,597 @@ func (c *Config) Send(input WebTransaction) Result { return result } + +func (c *Config) GetAllIPv4(searchStr string) ([]InfoIPv4, error) { + sessionID, err := randomStr() + if err != nil { + return nil, err + } + + cert, err := tls.LoadX509KeyPair(c.CertFilePath, c.KeyFilePath) + if err != nil { + return nil, err + } + + // Load CA + caCert, err := ioutil.ReadFile(c.CAFilePath) + if err != nil { + return nil, 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 nil, 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 nil, 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 { + return nil, 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 { + return nil, err + } + + resp, err = client.Post("https://iphostmaster.nic.ad.jp/jpnic/portalv4listmain.do", contentType, bytes.NewBuffer(strByte)) + if err != nil { + return nil, 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 { + return nil, 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 { + //return nil, 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:] + + return infos, nil +} + +func (c *Config) GetAllIPv6(searchStr string) ([]InfoIPv6, error) { + sessionID, err := randomStr() + if err != nil { + return nil, err + } + + cert, err := tls.LoadX509KeyPair(c.CertFilePath, c.KeyFilePath) + if err != nil { + return nil, err + } + + // Load CA + caCert, err := ioutil.ReadFile(c.CAFilePath) + if err != nil { + return nil, 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 nil, 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 nil, 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 { + return nil, err + } + + resp, err = client.Post("https://iphostmaster.nic.ad.jp/jpnic/K11310Action.do", contentType, bytes.NewBuffer(strByte)) + if err != nil { + return nil, 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 { + return nil, 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 { + //return nil, 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:] + + return infos, nil +} + +func (c *Config) GetIPv4User(userURL string) (InfoDetailIPv4, error) { + var info InfoDetailIPv4 + + 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() + + bodyStr, err := ioutil.ReadAll(resp.Body) + if err != nil { + return info, err + } + + doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr))) + 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) { + ioStr := strings.NewReader(tableCell4.Text()) + reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) + strByte, err := ioutil.ReadAll(reader) + if err != nil { + //return info, 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++ + } + }) + }) + }) + }) + }) + }) + }) + }) + }) + }) + }) + }) + + 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() + + bodyStr, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + + doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr))) + 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) { + ioStr := strings.NewReader(tableCell4.Text()) + reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) + strByte, err := ioutil.ReadAll(reader) + if err != nil { + //return info, 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++ + } + }) + }) + }) + }) + }) + }) + }) + }) + }) + }) + }) + }) + + return info, err +} diff --git a/jpnic_test.go b/jpnic_test.go index baea5f1..3a5d75c 100644 --- a/jpnic_test.go +++ b/jpnic_test.go @@ -1,7 +1,6 @@ package jpnic import ( - "bufio" "bytes" "crypto/tls" "crypto/x509" @@ -17,6 +16,19 @@ import ( "testing" ) +// 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" + +// 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" + //func TestResultProcess(t *testing.T) { // var result Result // @@ -102,9 +114,9 @@ import ( //func TestSend(t *testing.T) { // con := Config{ // URL: "https://iphostmaster.nic.ad.jp/webtrans/WebRegisterCtl", -// CertFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/v4-cert.pem", -// KeyFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/v4-prvkey.pem", -// CAFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/rootcacert_r3.cer", +// CertFilePath: certFilePath, +// KeyFilePath: keyFilePath, +// CAFilePath: caFilePath, // } // input := WebTransaction{} // @@ -122,9 +134,9 @@ func TestGetIPv4(t *testing.T) { con := Config{ URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/v4-cert.pem", - KeyFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/v4-prvkey.pem", - CAFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/rootcacert_r3.cer", + CertFilePath: certFilePath, + KeyFilePath: keyFilePath, + CAFilePath: caFilePath, } cert, err := tls.LoadX509KeyPair(con.CertFilePath, con.KeyFilePath) @@ -170,21 +182,6 @@ func TestGetIPv4(t *testing.T) { } defer resp.Body.Close() - t.Log(resp.Header) - t.Log("SESSION_ID:" + resp.Header.Get("Set-Cookie")[11:43]) - - scanner := bufio.NewScanner(resp.Body) - for scanner.Scan() { - ioStr := strings.NewReader(scanner.Text()) - reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) - strByte, err := ioutil.ReadAll(reader) - if err != nil { - t.Fatal(err) - } - - t.Logf("%s", string(strByte)) - } - sessionID = resp.Header.Get("Set-Cookie")[11:43] cookies = []*http.Cookie{ @@ -196,40 +193,15 @@ func TestGetIPv4(t *testing.T) { jar.SetCookies(urlObj, cookies) - //resp, err = client.Get("https://iphostmaster.nic.ad.jp/jpnic/membermenu.do") - //if err != nil { - // t.Fatal(err) - //} - //defer resp.Body.Close() - // - //t.Log(resp.Header) - ////t.Log("SESSION_ID:" + resp.Header.Get("Set-Cookie")[11:43]) - // - //scanner = bufio.NewScanner(resp.Body) - //for scanner.Scan() { - // ioStr := strings.NewReader(scanner.Text()) - // reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder()) - // strByte, err := ioutil.ReadAll(reader) - // if err != nil { - // t.Fatal(err) - // } - // - // t.Logf("%s", string(strByte)) - //} - resp, err = client.Get("https://iphostmaster.nic.ad.jp/jpnic/portalv4list.do") if err != nil { t.Fatal(err) } defer resp.Body.Close() - t.Log(resp.Header) - - scanner = bufio.NewScanner(resp.Body) - contentType := "application/x-www-form-urlencoded" - str := "destdisp=D12204&ipaddr=&sizeS=&sizeE=&netwrkName=®DateS=®DateE=&rtnDateS=&rtnDateE=&organizationName=&resceAdmSnm=HOMENOC&recepNo=&deliNo=" + 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()) @@ -244,8 +216,6 @@ func TestGetIPv4(t *testing.T) { } defer resp.Body.Close() - t.Log(resp.Header) - bodyStr, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) @@ -259,12 +229,11 @@ func TestGetIPv4(t *testing.T) { count := 0 var infos []InfoIPv4 - doc.Find("table").Each(func(index int, tablehtml *goquery.Selection) { - tablehtml.Find("tr").Each(func(indextr int, rowhtml *goquery.Selection) { + 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(indexth int, tablecell *goquery.Selection) { - //row = append(row, tablecell.Text()) - ioStr := strings.NewReader(tablecell.Text()) + 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 { @@ -272,10 +241,10 @@ func TestGetIPv4(t *testing.T) { } dataStr := strings.TrimSpace(string(strByte)) - switch indexth { + switch index { case 0: info.IPAddress = dataStr - info.DetailLink, _ = tablecell.Find("a").Attr("href") + info.DetailLink, _ = tableCell.Find("a").Attr("href") case 1: info.Size = dataStr case 2: @@ -321,9 +290,9 @@ func TestGetIPv6(t *testing.T) { con := Config{ URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do", - CertFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/v4-cert.pem", - KeyFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/v4-prvkey.pem", - CAFilePath: "/home/yonedayuto/Documents/HomeNOC/cert/rootcacert_r3.cer", + CertFilePath: certFilePath, + KeyFilePath: keyFilePath, + CAFilePath: caFilePath, } cert, err := tls.LoadX509KeyPair(con.CertFilePath, con.KeyFilePath) @@ -369,9 +338,6 @@ func TestGetIPv6(t *testing.T) { } defer resp.Body.Close() - t.Log(resp.Header) - t.Log("SESSION_ID:" + resp.Header.Get("Set-Cookie")[11:43]) - sessionID = resp.Header.Get("Set-Cookie")[11:43] cookies = []*http.Cookie{ @@ -385,7 +351,7 @@ func TestGetIPv6(t *testing.T) { contentType := "application/x-www-form-urlencoded" - str := "destdisp=D12204&ipaddr=&sizeS=&sizeE=&netwrkName=®DateS=®DateE=&rtnDateS=&rtnDateE=&organizationName=&resceAdmSnm=HOMENOC&recepNo=&deliNo=" + 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()) @@ -400,8 +366,6 @@ func TestGetIPv6(t *testing.T) { } defer resp.Body.Close() - t.Log(resp.Header) - bodyStr, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) @@ -415,12 +379,11 @@ func TestGetIPv6(t *testing.T) { count := 0 var infos []InfoIPv6 - doc.Find("table").Each(func(index int, tablehtml *goquery.Selection) { - tablehtml.Find("tr").Each(func(indextr int, rowhtml *goquery.Selection) { + 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(indexth int, tablecell *goquery.Selection) { - //row = append(row, tablecell.Text()) - ioStr := strings.NewReader(tablecell.Text()) + 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 { @@ -428,10 +391,10 @@ func TestGetIPv6(t *testing.T) { } dataStr := strings.TrimSpace(string(strByte)) - switch indexth { + switch index { case 0: info.IPAddress = dataStr - info.DetailLink, _ = tablecell.Find("a").Attr("href") + info.DetailLink, _ = tableCell.Find("a").Attr("href") case 1: info.NetworkName = dataStr case 2: @@ -464,3 +427,333 @@ func TestGetIPv6(t *testing.T) { 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, + 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))) + 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) + //} +} + +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, + 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/G11320.do?netwrk_id=2020021427992") + 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) + //} +}