API接口

一、API接口工作方式说明

我们的api接口支持两种工作方式。

1、 客户机在ipxe启动阶段我方程序主动向用户web接口请求安装所需数据。

此种方式工作优点是用户程序集中保存、管理数据。缺点是安装稳定性受到请求通讯影响较大。

功能:我方程序主动访问用户接口,可实现安装、日志、安装完成回调等数据请求。

2、用户首先向我方程序写入安装系统所需数据,然后用pxe引导机器,则自动进入流程。

此种工作方式可避免api通讯故障造成的安装失败影响,操作上稍微复杂点。

上图:api_gw为此功能打开选项。

功能:用户程序主动访问我方接口,可实现数据的读写删功能。

URL:http://ip:8789/api/gateway [ip为用户程序可访问到我方程序的地址,如果为内网网关需做tcp:8789端口转]。

3、请求接口支持ip白名单、https、AES加密三种安全措施

主动请求:支持https + AES 。

主动请求:支持https + AES + ip白名单。

4、api接口协议为:http + json

通讯协议:http,发数据:post,编码utf-8,数据类型为json


二、ipxe主动请求数据格式

主动请求模式,客户机每次ipxe引导时都会访问远程url。

1、ipxe请求安装列表
发:{"来源":"安装列表","pxe_mac":"AABBCCDDEEFF","命令":"get","区域":"江苏电信"} 来源:用户需要判断此值为【安装列表】,则为安装请求,此值也可能为【安装完成】等其他日志信息。 pxe_mac:根据些pxe_mac信息,用户判断是否需要安装系统 命令:get为读的意思,如果用户要安装系统,需要改写此值为set,如果不装,把接收到的json原样返回。 回: { "来源": "安装列表", "命令": "set", //--------此值为set 则读安装列表信息进行安装,否则本地系统启动 "区域": "电信机房01", "pxe_mac": "AABBCCDDEEFF", "安装列表": { "pxe_mac": "AABBCCDDEEFF", pxe启动网卡mac-----必选 "pxe_eth": "eth0", pxe启动网卡编号-----必选 "model_id": 1, 机器型号id---------必选 "os_id": 0, 需要安装系统的id---必选 "os_pwd": "Smart-idc", 系统密码-----------可选 "rdp": 3389, win远程端口--------可选 "ghoMode": "0", gho恢复模式,0=全盘,1=分区只格c "c_size": 50, c盘大小,全盘恢复有效 "ip": "", 公网ip地址 "网关": "", 网关信息 "dns": "" dns信息 } } ip地址格式:eth0,AABBCCDDEEFF,1.1.1.1,255.255.255.0;eth0,AABBCCDDEEFF,2.2.2.2,255.255.255.0; 网关格式:eth0,AABBCCDDEEFF,3.3.3.3 dns格式:eth0,AABBCCDDEEFF,223.5.5.5
2、安装完成
发: { "来源": "安装完成", "pxe_mac": "AABBCCDDEEFF", "区域": "电信机房01" } 用户根据pxe_mac判断此机器系统已经安装完成。 回: { "来源": "安装完成", "pxe_mac": "AABBCCDDEEFF", "区域": "电信机房01" }
三、api_gw 被动请求数据格式

被动请求模式,用户程序主动向我方程序发送数据。

请求url:http://1.2.3.4:8789/api/gateway

url中ip地址根据实际情况替换

1、api_gw 请求安装列表
本接口为数据访问接口,可实现读写删功能。 发: { "tbName": "安装列表", "命令": "get", "pxe_mac": "AABBCCDDEEFF" } tbName:表名,此处为【安装列表】。 命令: get,set,del pxe_mac:pxe启动网卡MAC 回: { "来源": "api_gw", "命令": "get", "区域": "电信机房01", "pxe_mac": "AABBCCDDEEFF", "tbName": "安装列表", "信息": "查找空", "安装列表": { "pxe_mac": "AABBCCDDEEFF", "pxe_eth": "eth0", "model_id": 1, "os_id": 0, "os_pwd": "Smart-idc", "rdp": 3389, "ghoMode": "0", "c_size": 50, "ip": "", "网关": "", "dns": "" } } ip地址格式:eth0,AABBCCDDEEFF,1.1.1.1,255.255.255.0;eth0,AABBCCDDEEFF,2.2.2.2,255.255.255.0; 网关格式:eth0,AABBCCDDEEFF,3.3.3.3 dns格式:eth0,AABBCCDDEEFF,223.5.5.5
2、请求型号列表
发: { "tbName": "型号列表" } 因为硬件及操作系统版本升级的原因,部份新版本系统已不在支持老的硬件。此接口用于检测我们 回: { "tbName": "型号列表", "count": 3, "型号列表": [ { "model_id": 1, "型号": "auto", "品牌": "其他", "备注": "" }, { "model_id": 2, "型号": "c6100", "品牌": "戴尔", "备注": "dell c6100" }, { "model_id": 3, "型号": "c5520", "品牌": "戴尔", "备注": "1" } ] }
3、请求系统列表
发: { "tbName": "系统列表" } 回: { "tbName": "系统列表", "count": 5, "系统列表": [ { "os_id": 1, "name": "win2003_r2_ent", "系统分类": "windows" }, { "os_id": 10, "name": "test", "系统分类": "windows" }, { "os_id": 101, "name": "win2008_r2_ent_64", "系统分类": "windows" }, { "os_id": 1103, "name": "centos_7.3.1611_64", "系统分类": "centos" }, { "os_id": 1106, "name": "centos_7.6.1810_64", "系统分类": "centos" } ] }
2、请求系统型号
发: { "tbName": "系统型号", "model_id": 1 } 回: { "tbName": "系统型号", "model_id": 1, "count": 3, "系统列表": [ { "os_id": 1, "name": "win2003_r2_ent", "系统分类": "windows" }, { "os_id": 101, "name": "win2008_r2_ent_64", "系统分类": "windows" }, { "os_id": 201, "name": "win2012_dc_r2_64", "系统分类": "windows" } ] }
四、AES加密解密程序示例
1、php程序的AES加密解密程序实例【仅支持php7.1以上版本】
$post_data = file_get_contents("php://input"); //接收POST数据 $keyAes = "1234567890123456";//------------此密码需要和我们方程序端密码一致 $aes = new Aes($keyAes); $decryptData = $aes->decrypt($post_data); //解密post接收到的加密数据 $encryptData = $aes->encrypt($decryptData); //加密要发送的数据 echo($encryptData); class Aes { /** * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得 */ protected $method; /** * var string $secret_key 加解密的密钥 */ protected $secret_key; /** * var string $iv 加解密的向量,有些方法需要设置比如CBC */ protected $iv; /** * var string $options (不知道怎么解释,目前设置为0没什么问题) */ protected $options; /** * 构造函数 * * @param string $key 密钥 * @param string $method 加密方式 * @param string $iv iv向量 * @param mixed $options 还不是很清楚 * */ public function __construct($key, $method = 'AES-128-CBC', $iv = '1234567890123456', $options = OPENSSL_RAW_DATA) { // key是必须要设置的 $this->secret_key = isset($key) ? $key : 'morefun'; $this->method = $method; $this->iv = $iv; $this->options = $options; } /** * 加密方法,对数据进行加密,返回加密后的数据 * * @param string $data 要加密的数据 * * @return string * */ public function encrypt($data) { $data1 = openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); return base64_encode($data1); } /** * 解密方法,对数据进行解密,返回解密后的数据 * * @param string $data 要解密的数据 * * @return string * */ public function decrypt($data) { $data = base64_decode($data); return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } }
2、c#程序AES加密解密程序示例
/// AES加密 /// text => 加密字符 /// password => 加密的密码 public static string api_AESEncrypt(string text, string password) { string iv = "1234567890123456"; RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(text); byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length); return Convert.ToBase64String(cipherBytes); } /// AES解密 /// text => 解密字符 /// password => 解密的密码 public static string api_AESDecrypt(string text, string password) { string iv = "1234567890123456"; RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(text); byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); }