this changes all api calls to respect the flags for tor and tor-port - config now has TorPort method to return flag value if set, or default - api now only has newClient, this creates a tor client when flag is present. does not need to be exported anymore - no methods take a tor argument as no longer needed - all commands now share the same behaviour logging tor messages - api.torClient was removed as not used anywhere - all calls to api.newClient now check for and return the error - api.HandlePost was removed as redundant of api.DoPostpull/41/head
@@ -13,26 +13,10 @@ import ( | |||||
cli "gopkg.in/urfave/cli.v1" | cli "gopkg.in/urfave/cli.v1" | ||||
) | ) | ||||
func client(userAgent string, tor bool) *writeas.Client { | |||||
var client *writeas.Client | |||||
if tor { | |||||
client = writeas.NewTorClient(TorPort) | |||||
} else { | |||||
if config.IsDev() { | |||||
client = writeas.NewDevClient() | |||||
} else { | |||||
client = writeas.NewClient() | |||||
} | |||||
} | |||||
client.UserAgent = userAgent | |||||
return client | |||||
} | |||||
func NewClient(c *cli.Context, authRequired bool) (*writeas.Client, error) { | |||||
func newClient(c *cli.Context, authRequired bool) (*writeas.Client, error) { | |||||
var client *writeas.Client | var client *writeas.Client | ||||
if config.IsTor(c) { | if config.IsTor(c) { | ||||
client = writeas.NewTorClient(TorPort) | |||||
client = writeas.NewTorClient(config.TorPort(c)) | |||||
} else { | } else { | ||||
if config.IsDev() { | if config.IsDev() { | ||||
client = writeas.NewDevClient() | client = writeas.NewDevClient() | ||||
@@ -54,8 +38,11 @@ func NewClient(c *cli.Context, authRequired bool) (*writeas.Client, error) { | |||||
// DoFetch retrieves the Write.as post with the given friendlyID, | // DoFetch retrieves the Write.as post with the given friendlyID, | ||||
// optionally via the Tor hidden service. | // optionally via the Tor hidden service. | ||||
func DoFetch(friendlyID, ua string, tor bool) error { | |||||
cl := client(ua, tor) | |||||
func DoFetch(c *cli.Context, friendlyID string) error { | |||||
cl, err := newClient(c, false) | |||||
if err != nil { | |||||
return err | |||||
} | |||||
p, err := cl.GetPost(friendlyID) | p, err := cl.GetPost(friendlyID) | ||||
if err != nil { | if err != nil { | ||||
@@ -72,9 +59,9 @@ func DoFetch(friendlyID, ua string, tor bool) error { | |||||
// DoFetchPosts retrieves all remote posts for the | // DoFetchPosts retrieves all remote posts for the | ||||
// authenticated user | // authenticated user | ||||
func DoFetchPosts(c *cli.Context) ([]writeas.Post, error) { | func DoFetchPosts(c *cli.Context) ([]writeas.Post, error) { | ||||
cl, err := NewClient(c, true) | |||||
cl, err := newClient(c, true) | |||||
if err != nil { | if err != nil { | ||||
return nil, err | |||||
return nil, fmt.Errorf("Unable to create client: %v", err) | |||||
} | } | ||||
posts, err := cl.GetUserPosts() | posts, err := cl.GetUserPosts() | ||||
@@ -87,8 +74,11 @@ func DoFetchPosts(c *cli.Context) ([]writeas.Post, error) { | |||||
// DoPost creates a Write.as post, returning an error if it was | // DoPost creates a Write.as post, returning an error if it was | ||||
// unsuccessful. | // unsuccessful. | ||||
func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) (*writeas.Post, error) { | |||||
cl, _ := NewClient(c, false) | |||||
func DoPost(c *cli.Context, post []byte, font string, encrypt, code bool) (*writeas.Post, error) { | |||||
cl, err := newClient(c, false) | |||||
if err != nil { | |||||
return nil, fmt.Errorf("Unable to create client: %v", err) | |||||
} | |||||
pp := &writeas.PostParams{ | pp := &writeas.PostParams{ | ||||
Font: config.GetFont(code, font), | Font: config.GetFont(code, font), | ||||
@@ -107,7 +97,7 @@ func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) ( | |||||
if p.Collection != nil { | if p.Collection != nil { | ||||
url = p.Collection.URL + p.Slug | url = p.Collection.URL + p.Slug | ||||
} else { | } else { | ||||
if tor { | |||||
if config.IsTor(c) { | |||||
url = config.TorBaseURL | url = config.TorBaseURL | ||||
} else if config.IsDev() { | } else if config.IsDev() { | ||||
url = config.DevBaseURL | url = config.DevBaseURL | ||||
@@ -143,10 +133,10 @@ func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) ( | |||||
// DoFetchCollections retrieves a list of the currently logged in users | // DoFetchCollections retrieves a list of the currently logged in users | ||||
// collections. | // collections. | ||||
func DoFetchCollections(c *cli.Context) ([]RemoteColl, error) { | func DoFetchCollections(c *cli.Context) ([]RemoteColl, error) { | ||||
cl, err := NewClient(c, true) | |||||
cl, err := newClient(c, true) | |||||
if err != nil { | if err != nil { | ||||
if config.Debug() { | if config.Debug() { | ||||
log.ErrorlnQuit("could not create new client: %v", err) | |||||
log.ErrorlnQuit("could not create client: %v", err) | |||||
} | } | ||||
return nil, fmt.Errorf("Couldn't create new client") | return nil, fmt.Errorf("Couldn't create new client") | ||||
} | } | ||||
@@ -156,7 +146,7 @@ func DoFetchCollections(c *cli.Context) ([]RemoteColl, error) { | |||||
if config.Debug() { | if config.Debug() { | ||||
log.ErrorlnQuit("failed fetching user collections: %v", err) | log.ErrorlnQuit("failed fetching user collections: %v", err) | ||||
} | } | ||||
return nil, fmt.Errorf("Couldn't get user collections") | |||||
return nil, fmt.Errorf("Couldn't get user blogs") | |||||
} | } | ||||
out := make([]RemoteColl, len(*colls)) | out := make([]RemoteColl, len(*colls)) | ||||
@@ -174,8 +164,11 @@ func DoFetchCollections(c *cli.Context) ([]RemoteColl, error) { | |||||
} | } | ||||
// DoUpdate updates the given post on Write.as. | // DoUpdate updates the given post on Write.as. | ||||
func DoUpdate(c *cli.Context, post []byte, friendlyID, token, font string, tor, code bool) error { | |||||
cl, _ := NewClient(c, false) | |||||
func DoUpdate(c *cli.Context, post []byte, friendlyID, token, font string, code bool) error { | |||||
cl, err := newClient(c, false) | |||||
if err != nil { | |||||
return fmt.Errorf("Unable to create client: %v", err) | |||||
} | |||||
params := writeas.PostParams{} | params := writeas.PostParams{} | ||||
params.Title, params.Content = posts.ExtractTitle(string(post)) | params.Title, params.Content = posts.ExtractTitle(string(post)) | ||||
@@ -186,27 +179,24 @@ func DoUpdate(c *cli.Context, post []byte, friendlyID, token, font string, tor, | |||||
params.Font = config.GetFont(code, font) | params.Font = config.GetFont(code, font) | ||||
} | } | ||||
_, err := cl.UpdatePost(friendlyID, token, ¶ms) | |||||
_, err = cl.UpdatePost(friendlyID, token, ¶ms) | |||||
if err != nil { | if err != nil { | ||||
if config.Debug() { | if config.Debug() { | ||||
log.ErrorlnQuit("Problem updating: %v", err) | log.ErrorlnQuit("Problem updating: %v", err) | ||||
} | } | ||||
return fmt.Errorf("Post doesn't exist, or bad edit token given.") | return fmt.Errorf("Post doesn't exist, or bad edit token given.") | ||||
} | } | ||||
if tor { | |||||
log.Info(c, "Post updated via hidden service.") | |||||
} else { | |||||
log.Info(c, "Post updated.") | |||||
} | |||||
return nil | return nil | ||||
} | } | ||||
// DoDelete deletes the given post on Write.as, and removes any local references | // DoDelete deletes the given post on Write.as, and removes any local references | ||||
func DoDelete(c *cli.Context, friendlyID, token string, tor bool) error { | |||||
cl, _ := NewClient(c, false) | |||||
func DoDelete(c *cli.Context, friendlyID, token string) error { | |||||
cl, err := newClient(c, false) | |||||
if err != nil { | |||||
return fmt.Errorf("Unable to create client: %v", err) | |||||
} | |||||
err := cl.DeletePost(friendlyID, token) | |||||
err = cl.DeletePost(friendlyID, token) | |||||
if err != nil { | if err != nil { | ||||
if config.Debug() { | if config.Debug() { | ||||
log.ErrorlnQuit("Problem deleting: %v", err) | log.ErrorlnQuit("Problem deleting: %v", err) | ||||
@@ -214,18 +204,16 @@ func DoDelete(c *cli.Context, friendlyID, token string, tor bool) error { | |||||
return fmt.Errorf("Post doesn't exist, or bad edit token given.") | return fmt.Errorf("Post doesn't exist, or bad edit token given.") | ||||
} | } | ||||
if tor { | |||||
log.Info(c, "Post deleted from hidden service.") | |||||
} else { | |||||
log.Info(c, "Post deleted.") | |||||
} | |||||
RemovePost(c.App.ExtraInfo()["configDir"], friendlyID) | RemovePost(c.App.ExtraInfo()["configDir"], friendlyID) | ||||
return nil | return nil | ||||
} | } | ||||
func DoLogIn(c *cli.Context, username, password string) error { | func DoLogIn(c *cli.Context, username, password string) error { | ||||
cl := client(config.UserAgent(c), config.IsTor(c)) | |||||
cl, err := newClient(c, false) | |||||
if err != nil { | |||||
return fmt.Errorf("Unable to create client: %v", err) | |||||
} | |||||
u, err := cl.LogIn(username, password) | u, err := cl.LogIn(username, password) | ||||
if err != nil { | if err != nil { | ||||
@@ -244,9 +232,9 @@ func DoLogIn(c *cli.Context, username, password string) error { | |||||
} | } | ||||
func DoLogOut(c *cli.Context) error { | func DoLogOut(c *cli.Context) error { | ||||
cl, err := NewClient(c, true) | |||||
cl, err := newClient(c, true) | |||||
if err != nil { | if err != nil { | ||||
return err | |||||
return fmt.Errorf("Unable to create client: %v", err) | |||||
} | } | ||||
err = cl.LogOut() | err = cl.LogOut() | ||||
@@ -60,7 +60,7 @@ func AddPost(c *cli.Context, id, token string) error { | |||||
// ClaimPost adds a local post to the authenticated user's account and deletes | // ClaimPost adds a local post to the authenticated user's account and deletes | ||||
// the local reference | // the local reference | ||||
func ClaimPosts(c *cli.Context, localPosts *[]Post) (*[]writeas.ClaimPostResult, error) { | func ClaimPosts(c *cli.Context, localPosts *[]Post) (*[]writeas.ClaimPostResult, error) { | ||||
cl, err := NewClient(c, true) | |||||
cl, err := newClient(c, true) | |||||
if err != nil { | if err != nil { | ||||
return nil, err | return nil, err | ||||
} | } | ||||
@@ -260,20 +260,6 @@ func WritePost(postsDir string, p *writeas.Post) error { | |||||
return ioutil.WriteFile(filepath.Join(postsDir, collDir, postFilename), []byte(txtFile), 0644) | return ioutil.WriteFile(filepath.Join(postsDir, collDir, postFilename), []byte(txtFile), 0644) | ||||
} | } | ||||
func HandlePost(fullPost []byte, c *cli.Context) (*writeas.Post, error) { | |||||
tor := config.IsTor(c) | |||||
if c.Int("tor-port") != 0 { | |||||
TorPort = c.Int("tor-port") | |||||
} | |||||
if tor { | |||||
log.Info(c, "Posting to hidden service...") | |||||
} else { | |||||
log.Info(c, "Posting...") | |||||
} | |||||
return DoPost(c, fullPost, c.String("font"), false, tor, c.Bool("code")) | |||||
} | |||||
func ReadStdIn() []byte { | func ReadStdIn() []byte { | ||||
numBytes, numChunks := int64(0), int64(0) | numBytes, numChunks := int64(0), int64(0) | ||||
r := bufio.NewReader(os.Stdin) | r := bufio.NewReader(os.Stdin) | ||||
@@ -28,12 +28,12 @@ func CmdPull(c *cli.Context) error { | |||||
syncSetUp(c.App.ExtraInfo()["configDir"], cfg) | syncSetUp(c.App.ExtraInfo()["configDir"], cfg) | ||||
} | } | ||||
// Fetch posts | |||||
cl, err := NewClient(c, true) | |||||
cl, err := newClient(c, true) | |||||
if err != nil { | if err != nil { | ||||
return err | return err | ||||
} | } | ||||
// Fetch posts | |||||
posts, err := cl.GetUserPosts() | posts, err := cl.GetUserPosts() | ||||
if err != nil { | if err != nil { | ||||
return err | return err | ||||
@@ -1,18 +0,0 @@ | |||||
package api | |||||
import ( | |||||
"fmt" | |||||
"net/http" | |||||
"code.as/core/socks" | |||||
) | |||||
var ( | |||||
TorPort = 9150 | |||||
) | |||||
func torClient() *http.Client { | |||||
dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, fmt.Sprintf("127.0.0.1:%d", TorPort)) | |||||
transport := &http.Transport{Dial: dialSocksProxy} | |||||
return &http.Client{Transport: transport} | |||||
} |
@@ -14,7 +14,13 @@ import ( | |||||
) | ) | ||||
func CmdPost(c *cli.Context) error { | func CmdPost(c *cli.Context) error { | ||||
_, err := api.HandlePost(api.ReadStdIn(), c) | |||||
_, err := api.DoPost(c, api.ReadStdIn(), c.String("font"), false, c.Bool("code")) | |||||
if config.IsTor(c) { | |||||
log.Info(c, "Posted to hidden service...") | |||||
} else { | |||||
log.Info(c, "Posted...") | |||||
} | |||||
return err | return err | ||||
} | } | ||||
@@ -35,7 +41,13 @@ func CmdNew(c *cli.Context) error { | |||||
log.InfolnQuit("Empty post. Bye!") | log.InfolnQuit("Empty post. Bye!") | ||||
} | } | ||||
_, err := api.HandlePost(*p, c) | |||||
if config.IsTor(c) { | |||||
log.Info(c, "Posting to hidden service...") | |||||
} else { | |||||
log.Info(c, "Posting...") | |||||
} | |||||
_, err := api.DoPost(c, *p, c.String("font"), false, c.Bool("code")) | |||||
if err != nil { | if err != nil { | ||||
log.Errorln("Error posting: %s\n%s", err, config.MessageRetryCompose(fname)) | log.Errorln("Error posting: %s\n%s", err, config.MessageRetryCompose(fname)) | ||||
return cli.NewExitError("", 1) | return cli.NewExitError("", 1) | ||||
@@ -58,7 +70,13 @@ func CmdPublish(c *cli.Context) error { | |||||
if err != nil { | if err != nil { | ||||
return err | return err | ||||
} | } | ||||
_, err = api.HandlePost(content, c) | |||||
if config.IsTor(c) { | |||||
log.Info(c, "Publishing to hidden service...") | |||||
} else { | |||||
log.Info(c, "Publishing...") | |||||
} | |||||
_, err = api.DoPost(c, content, c.String("font"), false, c.Bool("code")) | |||||
// TODO: write local file if directory is set | // TODO: write local file if directory is set | ||||
return err | return err | ||||
@@ -81,17 +99,13 @@ func CmdDelete(c *cli.Context) error { | |||||
} | } | ||||
} | } | ||||
tor := config.IsTor(c) | |||||
if c.Int("tor-port") != 0 { | |||||
api.TorPort = c.Int("tor-port") | |||||
} | |||||
if tor { | |||||
if config.IsTor(c) { | |||||
log.Info(c, "Deleting via hidden service...") | log.Info(c, "Deleting via hidden service...") | ||||
} else { | } else { | ||||
log.Info(c, "Deleting...") | log.Info(c, "Deleting...") | ||||
} | } | ||||
err := api.DoDelete(c, friendlyID, token, tor) | |||||
err := api.DoDelete(c, friendlyID, token) | |||||
if err != nil { | if err != nil { | ||||
return cli.NewExitError(fmt.Sprintf("Couldn't delete remote copy: %v", err), 1) | return cli.NewExitError(fmt.Sprintf("Couldn't delete remote copy: %v", err), 1) | ||||
} | } | ||||
@@ -120,17 +134,13 @@ func CmdUpdate(c *cli.Context) error { | |||||
// Read post body | // Read post body | ||||
fullPost := api.ReadStdIn() | fullPost := api.ReadStdIn() | ||||
tor := config.IsTor(c) | |||||
if c.Int("tor-port") != 0 { | |||||
api.TorPort = c.Int("tor-port") | |||||
} | |||||
if tor { | |||||
if config.IsTor(c) { | |||||
log.Info(c, "Updating via hidden service...") | log.Info(c, "Updating via hidden service...") | ||||
} else { | } else { | ||||
log.Info(c, "Updating...") | log.Info(c, "Updating...") | ||||
} | } | ||||
return api.DoUpdate(c, fullPost, friendlyID, token, c.String("font"), tor, c.Bool("code")) | |||||
return api.DoUpdate(c, fullPost, friendlyID, token, c.String("font"), c.Bool("code")) | |||||
} | } | ||||
func CmdGet(c *cli.Context) error { | func CmdGet(c *cli.Context) error { | ||||
@@ -139,17 +149,13 @@ func CmdGet(c *cli.Context) error { | |||||
return cli.NewExitError("usage: writeas get <postId>", 1) | return cli.NewExitError("usage: writeas get <postId>", 1) | ||||
} | } | ||||
tor := config.IsTor(c) | |||||
if c.Int("tor-port") != 0 { | |||||
api.TorPort = c.Int("tor-port") | |||||
} | |||||
if tor { | |||||
if config.IsTor(c) { | |||||
log.Info(c, "Getting via hidden service...") | log.Info(c, "Getting via hidden service...") | ||||
} else { | } else { | ||||
log.Info(c, "Getting...") | log.Info(c, "Getting...") | ||||
} | } | ||||
return api.DoFetch(friendlyID, config.UserAgent(c), tor) | |||||
return api.DoFetch(c, friendlyID) | |||||
} | } | ||||
func CmdAdd(c *cli.Context) error { | func CmdAdd(c *cli.Context) error { | ||||
@@ -223,6 +229,11 @@ func CmdCollections(c *cli.Context) error { | |||||
if u == nil { | if u == nil { | ||||
return cli.NewExitError("You must be authenticated to view collections.\nLog in first with: writeas auth <username>", 1) | return cli.NewExitError("You must be authenticated to view collections.\nLog in first with: writeas auth <username>", 1) | ||||
} | } | ||||
if config.IsTor(c) { | |||||
log.Info(c, "Getting blogs via hidden service...") | |||||
} else { | |||||
log.Info(c, "Getting blogs...") | |||||
} | |||||
colls, err := api.DoFetchCollections(c) | colls, err := api.DoFetchCollections(c) | ||||
if err != nil { | if err != nil { | ||||
return cli.NewExitError(fmt.Sprintf("Couldn't get collections for user %s: %v", u.User.Username, err), 1) | return cli.NewExitError(fmt.Sprintf("Couldn't get collections for user %s: %v", u.User.Username, err), 1) | ||||
@@ -260,6 +271,10 @@ func CmdClaim(c *cli.Context) error { | |||||
} | } | ||||
log.Info(c, "Claiming %d post(s) for %s...", len(*localPosts), u.User.Username) | log.Info(c, "Claiming %d post(s) for %s...", len(*localPosts), u.User.Username) | ||||
if config.IsTor(c) { | |||||
log.Info(c, "...via hidden service...") | |||||
} | |||||
results, err := api.ClaimPosts(c, localPosts) | results, err := api.ClaimPosts(c, localPosts) | ||||
if err != nil { | if err != nil { | ||||
return cli.NewExitError(fmt.Sprintf("Failed to claim posts: %v", err), 1) | return cli.NewExitError(fmt.Sprintf("Failed to claim posts: %v", err), 1) | ||||
@@ -313,6 +328,12 @@ func CmdAuth(c *cli.Context) error { | |||||
if len(pass) == 0 { | if len(pass) == 0 { | ||||
return cli.NewExitError("Please enter your password.", 1) | return cli.NewExitError("Please enter your password.", 1) | ||||
} | } | ||||
if config.IsTor(c) { | |||||
log.Info(c, "Logging in to hidden service...") | |||||
} else { | |||||
log.Info(c, "Logging in...") | |||||
} | |||||
err = api.DoLogIn(c, username, string(pass)) | err = api.DoLogIn(c, username, string(pass)) | ||||
if err != nil { | if err != nil { | ||||
return cli.NewExitError(fmt.Sprintf("error logging in: %v", err), 1) | return cli.NewExitError(fmt.Sprintf("error logging in: %v", err), 1) | ||||
@@ -322,5 +343,10 @@ func CmdAuth(c *cli.Context) error { | |||||
} | } | ||||
func CmdLogOut(c *cli.Context) error { | func CmdLogOut(c *cli.Context) error { | ||||
if config.IsTor(c) { | |||||
log.Info(c, "Logging out of hidden service...") | |||||
} else { | |||||
log.Info(c, "Logging out...") | |||||
} | |||||
return api.DoLogOut(c) | return api.DoLogOut(c) | ||||
} | } |
@@ -15,6 +15,7 @@ const ( | |||||
WriteasBaseURL = "https://write.as" | WriteasBaseURL = "https://write.as" | ||||
DevBaseURL = "https://development.write.as" | DevBaseURL = "https://development.write.as" | ||||
TorBaseURL = "http://writeas7pm7rcdqg.onion" | TorBaseURL = "http://writeas7pm7rcdqg.onion" | ||||
torPort = 9150 | |||||
) | ) | ||||
func UserAgent(c *cli.Context) string { | func UserAgent(c *cli.Context) string { | ||||
@@ -29,6 +30,13 @@ func IsTor(c *cli.Context) bool { | |||||
return c.Bool("tor") || c.Bool("t") | return c.Bool("tor") || c.Bool("t") | ||||
} | } | ||||
func TorPort(c *cli.Context) int { | |||||
if c.IsSet("tor-port") && c.Int("tor-port") != 0 { | |||||
return c.Int("tor-port") | |||||
} | |||||
return torPort | |||||
} | |||||
func Language(c *cli.Context, auto bool) string { | func Language(c *cli.Context, auto bool) string { | ||||
if l := c.String("lang"); l != "" { | if l := c.String("lang"); l != "" { | ||||
return l | return l | ||||