package extractor import ( "bufio" "io" "os" "strconv" "strings" "time" ) type staticCookieJar []Cookie // GetAll will return all cookies in the jar. func (s *staticCookieJar) GetAll() ([]Cookie, error) { return *s, nil } // Get will, given a URL, return all cookies that are valid for that URL. func (s *staticCookieJar) Get(target string) ([]Cookie, error) { var validCookies []Cookie for _, cookie := range *s { if match, err := cookie.IsTargetMatch(target); err != nil { return nil, err } else if match { validCookies = append(validCookies, cookie) } } return validCookies, nil } func (s *staticCookieJar) Set(cookie Cookie) error { // see if the cookie already exists for i, c := range *s { if c.Name == cookie.Name && c.Host == cookie.Host && c.Path == cookie.Path { (*s)[i] = cookie return nil } } *s = append(*s, cookie) return nil } func (s *staticCookieJar) Delete(cookie Cookie) error { for i, c := range *s { if c.Name == cookie.Name && c.Host == cookie.Host && c.Path == cookie.Path { *s = append((*s)[:i], (*s)[i+1:]...) return nil } } return nil } // LoadCookiesFile loads cookies from a file, in the format of cookies.txt. func LoadCookiesFile(path string) (CookieJar, error) { fp, err := os.Open(path) if err != nil { return nil, err } defer func(cl io.Closer) { _ = cl.Close() }(fp) var cookies staticCookieJar scanner := bufio.NewScanner(fp) for scanner.Scan() { line := scanner.Text() if line == "" { continue } if line[0] == '#' { continue } parts := strings.Split(line, "\t") if len(parts) < 7 { continue } expiry, err := strconv.ParseInt(parts[4], 10, 64) if err != nil { expiry = time.Now().Add(180 * 24 * time.Hour).Unix() // Default expiry } cookies = append(cookies, Cookie{ Host: parts[0], HttpOnly: strings.ToLower(parts[1]) == "true", Path: parts[2], Secure: strings.ToLower(parts[3]) == "true", Name: parts[5], Expires: time.Unix(expiry, 0), Value: parts[6], }) } return &cookies, nil }