Skip to content

Instantly share code, notes, and snippets.

@hacktivist123
Created January 9, 2025 15:15
Show Gist options
  • Save hacktivist123/3774c0138f84aae2b773f3b79540c73c to your computer and use it in GitHub Desktop.
Save hacktivist123/3774c0138f84aae2b773f3b79540c73c to your computer and use it in GitHub Desktop.
A simple web crawler
package main
import (
"bytes"
"crypto/tls"
"fmt"
"log"
"net/http/httptrace"
"time"
"net/http"
"github.com/gocolly/colly"
)
func main() {
colly.NewCollector(
colly.AllowedDomains("https://convocation.unilag.edu.ng"),
)
loginURL := "https://convocation.unilag.edu.ng/access"
loginData := map[string]string{
"username": "xxxx",
"password": "xxxx",
}
statusCode, err := submitForm(loginURL, loginData)
if err != nil {
fmt.Printf("Error submitting login form: %v\n", err)
return
}
fmt.Printf("Login form submission status: %d\n", statusCode)
}
// submitForm submits a form using HTTP POST and returns the status code
func submitForm(url string, formData map[string]string) (int, error) {
form := ""
for key, value := range formData {
form += fmt.Sprintf("%s=%s&", key, value)
}
form = form[:len(form)-1]
req, err := http.NewRequest("POST", url, bytes.NewBufferString(form))
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// cater for TLS handshake timeout errors with verbose logging
trace := &httptrace.ClientTrace{
TLSHandshakeStart: func() { log.Println("TLS handshake started") },
TLSHandshakeDone: func(state tls.ConnectionState, err error) { log.Println("TLS handshake done") },
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
client := &http.Client{Timeout: 60 * time.Second}
resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment