Remove API authentication
This commit is contained in:
		
							parent
							
								
									084bd51ac4
								
							
						
					
					
						commit
						184144eb1f
					
				| 
						 | 
				
			
			@ -1,10 +1,3 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	CONFIG_PLACEHOLDER string = "https://old.reddit.com/prefs/apps"
 | 
			
		||||
	USER_AGENT         string = "linux:omordl:v0.1.0 (by u/the_blank_x)"
 | 
			
		||||
	EXAMPLE_CONFIG     string = `{
 | 
			
		||||
	"client_id": "https://old.reddit.com/prefs/apps",
 | 
			
		||||
	"client_secret": "https://old.reddit.com/prefs/apps"
 | 
			
		||||
}`
 | 
			
		||||
)
 | 
			
		||||
const USER_AGENT string = "linux:omordl:v0.2.0 (by u/the_blank_x)"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										25
									
								
								main.go
								
								
								
								
							
							
						
						
									
										25
									
								
								main.go
								
								
								
								
							| 
						 | 
				
			
			@ -26,10 +26,15 @@ func main() {
 | 
			
		|||
		fmt.Fprintf(os.Stderr, "Error when parsing submission url: %s\n", err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
	submissionJsonUrl := ""
 | 
			
		||||
	submissionId := os.Args[1]
 | 
			
		||||
	if submissionUrl.Hostname() == "redd.it" {
 | 
			
		||||
		submissionJsonUrl = "https://old.reddit.com" + submissionUrl.EscapedPath() + ".json?raw_json=1&limit=1"
 | 
			
		||||
		submissionId = submissionUrl.EscapedPath()[1:]
 | 
			
		||||
	} else if submissionUrl.Hostname() != "" {
 | 
			
		||||
	} else if submissionUrl.Hostname() == "" {
 | 
			
		||||
		submissionJsonUrl = "https://old.reddit.com/" + os.Args[1] + ".json?raw_json=1&limit=1"
 | 
			
		||||
	} else {
 | 
			
		||||
		submissionJsonUrl = "https://old.reddit.com" + submissionUrl.EscapedPath() + "/.json?raw_json=1&limit=1"
 | 
			
		||||
		split := strings.SplitN(submissionUrl.EscapedPath(), "/", 6)
 | 
			
		||||
		if len(split) < 5 {
 | 
			
		||||
			fmt.Fprintln(os.Stderr, "URL passed does not have enough path seperators")
 | 
			
		||||
| 
						 | 
				
			
			@ -37,22 +42,8 @@ func main() {
 | 
			
		|||
		}
 | 
			
		||||
		submissionId = split[4]
 | 
			
		||||
	}
 | 
			
		||||
	if submissionId == "" {
 | 
			
		||||
		fmt.Fprintln(os.Stderr, "Submission ID is empty")
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
	client := &http.Client{}
 | 
			
		||||
	err = LoadConfigAndData()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Fprintf(os.Stderr, "Failed to load config and data: %s\n", err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
	token, err := GetToken(client)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Fprintf(os.Stderr, "Failed to get token: %s\n", err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
	submission, err := GetSubmission(client, token, submissionId)
 | 
			
		||||
	submission, err := GetSubmission(client, submissionJsonUrl)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Fprintf(os.Stderr, "Failed to get submission: %s\n", err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
| 
						 | 
				
			
			@ -62,7 +53,7 @@ func main() {
 | 
			
		|||
		filename = "_" + filename[1:]
 | 
			
		||||
	}
 | 
			
		||||
	if submission.CrosspostParent != "" && len(submission.CrosspostParentList) > 0 {
 | 
			
		||||
		submission, err = GetSubmission(client, token, submission.CrosspostParent[3:])
 | 
			
		||||
		submission, err = GetSubmission(client, "https://old.reddit.com"+submission.CrosspostParentList[0].Permalink+".json?raw_json=1&limit=1")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Fprintf(os.Stderr, "Failed to get original submission: %s\n", err)
 | 
			
		||||
			os.Exit(1)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										29
									
								
								structs.go
								
								
								
								
							
							
						
						
									
										29
									
								
								structs.go
								
								
								
								
							| 
						 | 
				
			
			@ -1,21 +1,5 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
type Config struct {
 | 
			
		||||
	ClientId     string `json:"client_id"`
 | 
			
		||||
	ClientSecret string `json:"client_secret"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Data struct {
 | 
			
		||||
	AuthorizationHeader string `json:"authorization_header"`
 | 
			
		||||
	AuthorizationExpiry int64  `json:"authorization_expiry"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type TokenResponse struct {
 | 
			
		||||
	AccessToken string `json:"access_token"`
 | 
			
		||||
	TokenType   string `json:"token_type"`
 | 
			
		||||
	ExpiresIn   int64  `json:"expires_in"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GalleryData struct {
 | 
			
		||||
	Items []struct {
 | 
			
		||||
		Id      int    `json:"id"`
 | 
			
		||||
| 
						 | 
				
			
			@ -38,12 +22,13 @@ type PreviewSource struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
type Submission struct {
 | 
			
		||||
	CrosspostParent string `json:"crosspost_parent"`
 | 
			
		||||
	// we don't care about the list, just does it exist or not
 | 
			
		||||
	CrosspostParentList []interface{} `json:"crosspost_parent_list"`
 | 
			
		||||
	Url                 string        `json:"url"`
 | 
			
		||||
	IsVideo             bool          `json:"is_video"`
 | 
			
		||||
	SecureMedia         struct {
 | 
			
		||||
	CrosspostParent     string `json:"crosspost_parent"`
 | 
			
		||||
	CrosspostParentList []struct {
 | 
			
		||||
		Permalink string `json:"permalink"`
 | 
			
		||||
	} `json:"crosspost_parent_list"`
 | 
			
		||||
	Url         string `json:"url"`
 | 
			
		||||
	IsVideo     bool   `json:"is_video"`
 | 
			
		||||
	SecureMedia struct {
 | 
			
		||||
		RedditVideo struct {
 | 
			
		||||
			HlsUrl      string `json:"hls_url"`
 | 
			
		||||
			DashUrl     string `json:"dash_url"`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										118
									
								
								utils.go
								
								
								
								
							
							
						
						
									
										118
									
								
								utils.go
								
								
								
								
							| 
						 | 
				
			
			@ -6,74 +6,13 @@ import (
 | 
			
		|||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
	"mime"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	configDir    string
 | 
			
		||||
	config       *Config
 | 
			
		||||
	data         *Data
 | 
			
		||||
	mimeOverride map[string]string
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func LoadConfigAndData() error {
 | 
			
		||||
	iHateBugs, err := os.UserConfigDir()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("Cannot determine config directory: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	configDir = filepath.Join(iHateBugs, "omordl")
 | 
			
		||||
	err = os.MkdirAll(configDir, 0o700)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("Cannot create config directory: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	configFile, err := os.OpenFile(filepath.Join(configDir, "config.json"), os.O_RDONLY, 0o600)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		contents, err := io.ReadAll(configFile)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Failed to read config file: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
		err = json.Unmarshal(contents, &config)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Failed to parse config file: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
	} else if errors.Is(err, fs.ErrNotExist) {
 | 
			
		||||
		configFile, err = os.OpenFile(filepath.Join(configDir, "config.json"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Failed to open config file for writing default template: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
		_, err = configFile.WriteString(EXAMPLE_CONFIG)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Failed to write to config file: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
		return fmt.Errorf("Please fill out the default template at %s", filepath.Join(configDir, "config.json"))
 | 
			
		||||
	} else {
 | 
			
		||||
		return fmt.Errorf("Failed to open config file: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	if config.ClientId == CONFIG_PLACEHOLDER || config.ClientSecret == CONFIG_PLACEHOLDER {
 | 
			
		||||
		return fmt.Errorf("Please fill out the default template at %s", filepath.Join(configDir, "config.json"))
 | 
			
		||||
	}
 | 
			
		||||
	dataFile, err := os.OpenFile(filepath.Join(configDir, "data.json"), os.O_RDONLY, 0o600)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		contents, err := io.ReadAll(dataFile)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Failed to read data file: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
		err = json.Unmarshal(contents, &data)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Failed to parse data file: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
	} else if !errors.Is(err, fs.ErrNotExist) {
 | 
			
		||||
		return fmt.Errorf("Failed to open data file: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
var mimeOverride map[string]string
 | 
			
		||||
 | 
			
		||||
// a jpeg file can have a jpe extension and i personally don't like it
 | 
			
		||||
// https://github.com/LonamiWebs/Telethon/blob/2e1be01ad4f6462de2e9e1f96a33537e51f44980/telethon/utils.py#L33
 | 
			
		||||
| 
						 | 
				
			
			@ -99,63 +38,12 @@ func LoadMimetypes() {
 | 
			
		|||
	mimeOverride["audio/flac"] = ".flac"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func WriteData() error {
 | 
			
		||||
	contents, err := json.Marshal(data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	file, err := os.OpenFile(filepath.Join(configDir, "data.json"), os.O_WRONLY|os.O_CREATE, 0o600)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	_, err = file.Write(contents)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetToken(client *http.Client) (string, error) {
 | 
			
		||||
	if data.AuthorizationHeader != "" && data.AuthorizationExpiry != 0 && data.AuthorizationExpiry > time.Now().Unix() {
 | 
			
		||||
		return data.AuthorizationHeader, nil
 | 
			
		||||
	}
 | 
			
		||||
	request, err := http.NewRequest("POST", "https://www.reddit.com/api/v1/access_token?grant_type=client_credentials", nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", fmt.Errorf("Failed to create request: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	request.Header.Add("User-Agent", USER_AGENT)
 | 
			
		||||
	request.SetBasicAuth(config.ClientId, config.ClientSecret)
 | 
			
		||||
	response, err := client.Do(request)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", fmt.Errorf("Failed to get response: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	contents, err := io.ReadAll(response.Body)
 | 
			
		||||
	response.Body.Close()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", fmt.Errorf("Failed to read response body: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	if response.StatusCode != 200 {
 | 
			
		||||
		return "", fmt.Errorf("Response returned status code %d, body: %s", response.StatusCode, contents)
 | 
			
		||||
	}
 | 
			
		||||
	var tokenResponse TokenResponse
 | 
			
		||||
	err = json.Unmarshal(contents, &tokenResponse)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", fmt.Errorf("Failed to parse response: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	s := []string{tokenResponse.TokenType, tokenResponse.AccessToken}
 | 
			
		||||
	data.AuthorizationHeader = strings.Join(s, " ")
 | 
			
		||||
	data.AuthorizationExpiry = time.Now().Unix() + tokenResponse.ExpiresIn - 5
 | 
			
		||||
	err = WriteData()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Fprintf(os.Stderr, "Warning: Failed to save token to disk: %s\n", err)
 | 
			
		||||
	}
 | 
			
		||||
	return data.AuthorizationHeader, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetSubmission(client *http.Client, token, submissionId string) (*Submission, error) {
 | 
			
		||||
	request, err := http.NewRequest("GET", "https://oauth.reddit.com/comments/"+submissionId+"/?raw_json=1&limit=1", nil)
 | 
			
		||||
func GetSubmission(client *http.Client, submissionJsonUrl string) (*Submission, error) {
 | 
			
		||||
	request, err := http.NewRequest("GET", submissionJsonUrl, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("Failed to create request: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	request.Header.Add("User-Agent", USER_AGENT)
 | 
			
		||||
	request.Header.Add("Authorization", data.AuthorizationHeader)
 | 
			
		||||
	response, err := client.Do(request)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("Failed to get response: %s", err)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in New Issue