agi.http.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package agi
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "log"
  9. "net/http"
  10. "net/url"
  11. "path/filepath"
  12. "github.com/robertkrimen/otto"
  13. "imuslab.com/arozos/mod/agi/static"
  14. )
  15. /*
  16. AJGI HTTP Request Library
  17. This is a library for allowing AGI script to make HTTP Request from the VM
  18. Returning either the head or the body of the request
  19. Author: tobychui
  20. */
  21. func (g *Gateway) HTTPLibRegister() {
  22. err := g.RegisterLib("http", g.injectHTTPFunctions)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. }
  27. func (g *Gateway) injectHTTPFunctions(payload *static.AgiLibInjectionPayload) {
  28. vm := payload.VM
  29. u := payload.User
  30. //scriptFsh := payload.ScriptFsh
  31. //scriptPath := payload.ScriptPath
  32. w := payload.Writer
  33. //r := payload.Request
  34. vm.Set("_http_get", func(call otto.FunctionCall) otto.Value {
  35. //Get URL from function variable
  36. url, err := call.Argument(0).ToString()
  37. if err != nil {
  38. return otto.NullValue()
  39. }
  40. //Get respond of the url
  41. res, err := http.Get(url)
  42. if err != nil {
  43. return otto.NullValue()
  44. }
  45. bodyContent, err := io.ReadAll(res.Body)
  46. if err != nil {
  47. return otto.NullValue()
  48. }
  49. returnValue, err := vm.ToValue(string(bodyContent))
  50. if err != nil {
  51. return otto.NullValue()
  52. }
  53. return returnValue
  54. })
  55. vm.Set("_http_post", func(call otto.FunctionCall) otto.Value {
  56. //Get URL from function paramter
  57. url, err := call.Argument(0).ToString()
  58. if err != nil {
  59. return otto.NullValue()
  60. }
  61. //Get JSON content from 2nd paramter
  62. sendWithPayload := true
  63. jsonContent, err := call.Argument(1).ToString()
  64. if err != nil {
  65. //Disable the payload send
  66. sendWithPayload = false
  67. }
  68. //Create the request
  69. var req *http.Request
  70. if sendWithPayload {
  71. req, _ = http.NewRequest("POST", url, bytes.NewBuffer([]byte(jsonContent)))
  72. } else {
  73. req, _ = http.NewRequest("POST", url, bytes.NewBuffer([]byte("")))
  74. }
  75. req.Header.Set("Content-Type", "application/json")
  76. req.Header.Set("User-Agent", "arozos-http-client/1.1")
  77. //Send the request
  78. client := &http.Client{}
  79. resp, err := client.Do(req)
  80. if err != nil {
  81. log.Println(err)
  82. return otto.NullValue()
  83. }
  84. defer resp.Body.Close()
  85. bodyContent, err := io.ReadAll(resp.Body)
  86. if err != nil {
  87. return otto.NullValue()
  88. }
  89. returnValue, _ := vm.ToValue(string(bodyContent))
  90. return returnValue
  91. })
  92. vm.Set("_http_head", func(call otto.FunctionCall) otto.Value {
  93. //Get URL from function paramter
  94. url, err := call.Argument(0).ToString()
  95. if err != nil {
  96. return otto.NullValue()
  97. }
  98. //Request the url
  99. resp, err := http.Get(url)
  100. if err != nil {
  101. return otto.NullValue()
  102. }
  103. headerKey, err := call.Argument(1).ToString()
  104. if err != nil || headerKey == "undefined" {
  105. //No headkey set. Return the whole header as JSON
  106. js, _ := json.Marshal(resp.Header)
  107. returnValue, _ := vm.ToValue(string(js))
  108. return returnValue
  109. } else {
  110. //headerkey is set. Return if exists
  111. possibleValue := resp.Header.Get(headerKey)
  112. js, _ := json.Marshal(possibleValue)
  113. returnValue, _ := vm.ToValue(string(js))
  114. return returnValue
  115. }
  116. })
  117. //Get target status code for response
  118. vm.Set("_http_code", func(call otto.FunctionCall) otto.Value {
  119. //Get URL from function paramter
  120. url, err := call.Argument(0).ToString()
  121. if err != nil {
  122. return otto.FalseValue()
  123. }
  124. req, err := http.NewRequest("GET", url, nil)
  125. if err != nil {
  126. g.RaiseError(err)
  127. return otto.FalseValue()
  128. }
  129. payload := ""
  130. client := new(http.Client)
  131. client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
  132. //Redirection. Return the target location as well
  133. dest, _ := req.Response.Location()
  134. payload = dest.String()
  135. return errors.New("Redirect")
  136. }
  137. response, err := client.Do(req)
  138. if err != nil {
  139. return otto.FalseValue()
  140. }
  141. defer client.CloseIdleConnections()
  142. vm.Run(`var _location = "` + payload + `";`)
  143. value, _ := otto.ToValue(response.StatusCode)
  144. return value
  145. })
  146. vm.Set("_http_download", func(call otto.FunctionCall) otto.Value {
  147. //Get URL from function paramter
  148. downloadURL, err := call.Argument(0).ToString()
  149. if err != nil {
  150. return otto.FalseValue()
  151. }
  152. decodedURL, _ := url.QueryUnescape(downloadURL)
  153. //Get download desintation from paramter
  154. vpath, err := call.Argument(1).ToString()
  155. if err != nil {
  156. return otto.FalseValue()
  157. }
  158. //Optional: filename paramter
  159. filename, err := call.Argument(2).ToString()
  160. if err != nil || filename == "undefined" {
  161. //Extract the filename from the url instead
  162. filename = filepath.Base(decodedURL)
  163. }
  164. //Check user acess permission
  165. if !u.CanWrite(vpath) {
  166. g.RaiseError(errors.New("Permission Denied"))
  167. return otto.FalseValue()
  168. }
  169. //Convert the vpath to realpath. Check if it exists
  170. fsh, rpath, err := static.VirtualPathToRealPath(vpath, u)
  171. if err != nil {
  172. return otto.FalseValue()
  173. }
  174. if !fsh.FileSystemAbstraction.FileExists(rpath) || !fsh.FileSystemAbstraction.IsDir(rpath) {
  175. g.RaiseError(errors.New(vpath + " is a file not a directory."))
  176. return otto.FalseValue()
  177. }
  178. downloadDest := filepath.Join(rpath, filename)
  179. //Ok. Download the file
  180. resp, err := http.Get(decodedURL)
  181. if err != nil {
  182. return otto.FalseValue()
  183. }
  184. defer resp.Body.Close()
  185. // Create the file
  186. err = fsh.FileSystemAbstraction.WriteStream(downloadDest, resp.Body, 0775)
  187. if err != nil {
  188. return otto.FalseValue()
  189. }
  190. return otto.TrueValue()
  191. })
  192. vm.Set("_http_getb64", func(call otto.FunctionCall) otto.Value {
  193. //Get URL from function variable and return bytes as base64
  194. url, err := call.Argument(0).ToString()
  195. if err != nil {
  196. return otto.NullValue()
  197. }
  198. //Get respond of the url
  199. res, err := http.Get(url)
  200. if err != nil {
  201. return otto.NullValue()
  202. }
  203. bodyContent, err := io.ReadAll(res.Body)
  204. if err != nil {
  205. return otto.NullValue()
  206. }
  207. sEnc := base64.StdEncoding.EncodeToString(bodyContent)
  208. r, err := otto.ToValue(string(sEnc))
  209. if err != nil {
  210. log.Println(err.Error())
  211. return otto.NullValue()
  212. }
  213. return r
  214. })
  215. vm.Set("_http_redirect", func(call otto.FunctionCall) otto.Value {
  216. //Redirect the current request to another url
  217. targetUrl, err := call.Argument(0).ToString()
  218. if err != nil {
  219. return otto.NullValue()
  220. }
  221. statusCode, err := call.Argument(1).ToInteger()
  222. if err != nil {
  223. //Default: Temporary redirect
  224. statusCode = 307
  225. }
  226. w.Header().Set("Location", targetUrl)
  227. w.WriteHeader(int(statusCode))
  228. return otto.TrueValue()
  229. })
  230. //Wrap all the native code function into an imagelib class
  231. vm.Run(`
  232. var http = {};
  233. http.get = _http_get;
  234. http.post = _http_post;
  235. http.head = _http_head;
  236. http.download = _http_download;
  237. http.getb64 = _http_getb64;
  238. http.getCode = _http_code;
  239. http.redirect = function(t, c){
  240. if (typeof(c) == "undefined"){
  241. c = 307;
  242. }
  243. _http_redirect(t,c);
  244. };
  245. `)
  246. }