agi.http.go 6.4 KB

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