前兩天零零碎碎看完了golang的基礎,想著找個小項目練練手,可是出現了一個十分棘手的問題
我要做的東西是網站路徑爆破
所以我會從文本字典中把一行行路徑讀取然后與域名拼接,但是我在跑起程序后出現了問題
下面是一個小片段
400 Bad Request-----http://www.xxx.com/channel.asp
400 Bad Request-----http://www.xxx.com/index.asp
404 Not Found-----http://www.xxx.com/admin.asp
程序本身并沒有錯誤,但是運行結果就比較怪了
Bad Request?
這并不是我要說的重點,我發現的問題是,除了最后一個地址,前面所有的地址都會顯示位400 Bad Request
經過幾輪測試,我覺得應該是網址拼接上出了問題
我的拼接函數是這樣
func ReturnBurstURL(fURL *os.File, baseurl string) (urlList []string) {
allURLTxt := bufio.NewReader(fURL)
for {
urlpath, readerError := allURLTxt.ReadString('\n')
newurl := baseurl + strings.Replace(urlpath, "\n", "", -1)
urlList = append(urlList, newurl)
if readerError == io.EOF {
fmt.Printf("\n讀取字典完成,準備開始,請等待...\n")
return urlList
}
}
}
我把取一行的方式換成bufio.NewScanner就正常了
func ReturnBurstURL(fURL *os.File, baseurl string) (urlList []string) {
allURLTxt := bufio.NewScanner(fURL)
for allURLTxt.Scan() {
newurl := baseurl + allURLTxt.Text()
urlList = append(urlList, newurl)
}
fmt.Printf("\n讀取字典完成,準備開始,請等待...\n")
return urlList
}
網上讀取文件一行很多人寫的文章是第一種方法,但是我也不知道什么問題導致這種情況的發生
我特地去查了查api文檔
func NewReader(rd io.Reader) *Reader
//NewReader returns a new Reader whose buffer has the default size.
func (b *Reader) ReadString(delim byte) (string, error)
//ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString returns err != nil if and only if the returned data does not end in delim. For simple uses, a Scanner may be more convenient.
func NewScanner(r io.Reader) *Scanner
//NewScanner returns a new Scanner to read from r. The split function defaults to ScanLines.
func (s *Scanner) Scan() bool
//Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method. It returns false when the scan stops, either by reaching the end of the input or an error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil. Scan panics if the split function returns 100 empty tokens without advancing the input. This is a common error mode for scanners.
func (s *Scanner) Text() string
//Text returns the most recent token generated by a call to Scan as a newly allocated string holding its bytes.
按照上面的api文檔,這兩個的區別就是兩者在返回string的時候,一個是數據+分隔符,一個是一行的數據,不帶分隔符
雖說我第一種方法也用strings.Replace方法把"\n"替換成了""空字符,但是可能還是有點奇奇怪怪的東西